Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

compare 2 rows next each other Expand / Collapse
Author
Message
Posted Monday, February 17, 2014 6:17 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, December 14, 2015 4:54 AM
Points: 120, Visits: 635
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.

Post #1542324
Posted Monday, February 17, 2014 6:35 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: 2 days ago @ 4:46 PM
Points: 19,907, Visits: 18,123
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
Post #1542327
Posted Tuesday, February 18, 2014 3:04 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 1:44 PM
Points: 40,973, Visits: 38,267
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."

Helpful Links:
How to post code problems
How to post performance problems
Post #1542770
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse