Sorry, I want to know which dates you are referring to. For example, for 2018-03-26, need to calculate the interval between it and 2018-05-21, or need to calculate the interval between it and each subsequent date.
Maybe this is what you want
CREATE TABLE #test(ID INT,[Date] DATE)
INSERT INTO #test VALUES
(4,'2018-03-26 00:00:00.000'),
(4,'2018-05-21 00:00:00.000'),
(4,'2018-06-25 00:00:00.000'),
(4,'2018-09-17 00:00:00.000'),
(4,'2019-05-13 00:00:00.000'),
(4,'2019-06-17 00:00:00.000'),
(4,'2019-07-22 00:00:00.000'),
(4,'2019-12-09 00:00:00.000'),
(4,'2021-03-03 00:00:00.000')
;WITH cte
as(SELECT *,LEAD([Date],1) OVER(ORDER BY [Date]) [Date2]
FROM #test)
SELECT *,ABS(DATEDIFF(day,[Date],[Date2])) datedif
FROM cte
WHERE ABS(DATEDIFF(day,[Date],[Date2]))>60
If you have any question, please feel free to let me know.
Regards
Echo
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
I mean minimum difference between dates should be 60 days
4 2018-03-26 00:00:00.000
4 2018-05-21 00:00:00.000 --does not qualify as difference is only 56 days
4 2018-06-25 00:00:00.000-- so we have to select this one directly after first date
4 2018-09-17 00:00:00.000--qualify
4 2019-05-13 00:00:00.000--qualify
4 2019-06-17 00:00:00.000--does not qualify as difference is only 35 days from last qualifid date
4 2019-07-22 00:00:00.000-- so we have to select this after 2019-05-13
4 2019-12-09 00:00:00.000--qualify
4 2021-03-03 00:00:00.000--qualify
4 2018-03-26 00:00:00.000
4 2018-05-21 00:00:00.000 --does not qualify as difference is only 56 days
4 2018-06-25 00:00:00.000-- so we have to select this one directly after first date
4 2018-09-17 00:00:00.000--qualify
4 2019-05-13 00:00:00.000--qualify
4 2019-06-17 00:00:00.000--does not qualify as difference is only 35 days from last qualifid date
4 2019-07-22 00:00:00.000-- so we have to select this after 2019-05-13
4 2019-12-09 00:00:00.000--qualify
4 2021-03-03 00:00:00.000--qualify
Sign in to comment