Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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: Thursday, November 20, 2014 4:16 AM
Points: 106, Visits: 469
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: Today @ 7:55 AM
Points: 18,060, Visits: 16,090
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-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:12 AM
Points: 35,770, Visits: 32,434
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

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

Add to briefcase

Permissions Expand / Collapse