Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


compare 2 rows next each other


compare 2 rows next each other

Author
Message
scottichrosaviakosmos
scottichrosaviakosmos
SSC-Enthusiastic
SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)

Group: General Forum Members
Points: 120 Visits: 652
create table dbo.abc(id varchar(10), version int, stat varchar(10),typo varchar(10),enterdate datetime)

insert into abc(id,version,stat,typo,enterdate)

select 100,10,'live','yes',getdate()
union
select 100,10,'live','yes',getdate()-1
union
select 100,10,'live','no',getdate()-2
union
select 100,10,'nonlive','yes',getdate()
union
select 101,9,'live','no',getdate()
union
select 101,8,'live','yes',getdate()-2
union
select 101,11,'nonlive','yes',getdate()

i want to get an output on below conditions:

1. only live data
2. find the current day id and compare with it recent previous live date.
a. if the currunt and previous stat is different then show data.
b. if current date and immediate previous live stat is same (eg. both are yes or both are no) ten do not show ay data for the that id.

this query will be run daily to check currunt day data with recent previous data.
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21093 Visits: 18259
try this and see if it works



SELECT *
FROM abc a1
INNER JOIN abc a2
ON a1.id = a2.id
AND a1.enterdate = a2.enterdate+1
AND a1.stat = a2.stat
WHERE a1.stat = 'Live'
AND a1.typo <> a2.typo;






Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45153 Visits: 39923
Post withdrawn. I thought this was going to turn out to be a massive double triangular join but a second look-see says it probably won't.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search