Friday, October 24, 2014

Compare dates between 2 different rows and columns

You can use one of the below methods


if sql 2012 or above



SELECT *
FROM
(
SELECT *,LEAD(Effective_Date,1) OVER (PARTITION BY Customer_ID ORDER BY ROWID) AS NxtDate
FROM Table
)t
WHERE NxtDate < Discontinue_Date


if earlier version



;With CTE
AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY Customer_ID ORDER BY ROWID) AS Seq,*
FROM Test
)

SELECT c1.ROWID ,
c1.Customer_ID ,
c1.PART_ID ,
c1.Effective_Date ,
c1.Discontinue_Date
FROM CTE c1
INNER JOIN CtE c2
ON c2.Seq = c1.Seq + 1
AND c2.Customer_ID = c1.Customer_ID
WHERE c2.Effective_Date < c1.Discontinue_Date





Please Mark This As Answer if it solved your issue

Please Mark This As Helpful if it helps to solve your issue

Visakh

----------------------------

My MSDN Page

My Personal Blog

My Facebook Page


No comments:

Post a Comment