Compare Dates in an iterative manner for a particular set of records –SQL Server 2005 Approach


I came across an article with the title from SQL Server Central with a solution for SQL Server 2000: http://www.sqlservercentral.com/articles/T-SQL/64555/
It would be interesting to see a solution for SQL Server 2005 solution. It seems this SQL Server solution is outperforming the CROSS JOIN solution.
Here is the code:

 

;WITH mycte

AS

 

(

SELECT A.PersonID,

CASE

WHENSUM(CASEWHEN A.rnA.Version<>0 THEN 1 ELSE 0 END)>0 THEN 1 ELSE 0 ENDas [Is ImProper]

FROM

(

SELECT

*,ROW_NUMBER()OVER(PARTITIONBY PersonID ORDERBY DEDate)-1 as rn

FROM

PersonRecord) A

GROUP

BY A.PersonID)

SELECT

B.PersonID, B.Version, B.DEDate, [Is ImProper]

FROM

PersonRecord B LEFTJOIN mycte ON b.PersonID=mycte.PersonID

 
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s