SQL Clone
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
Old Hand
Old Hand (334 reputation)Old Hand (334 reputation)Old Hand (334 reputation)Old Hand (334 reputation)Old Hand (334 reputation)Old Hand (334 reputation)Old Hand (334 reputation)Old Hand (334 reputation)

Group: General Forum Members
Points: 334 Visits: 691
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 Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66047 Visits: 18570
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 Guru
SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)SSC Guru (213K reputation)

Group: General Forum Members
Points: 213031 Visits: 41977
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

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