Hi @MuralidharTangudu-6301,
Please refer below:
DROP TABLE IF EXISTS [user]
create table [user]
(username varchar(20),
startdate date,
enddate date
)
insert into [user] values
('A','2020-02-03','2020-03-16'),
('A','2020-03-23','2020-03-28'),
('A','2020-04-03','2020-05-16'),
('A','2020-08-03','2020-09-16'),
('B','2020-01-01','2020-01-16'),
('B','2020-04-03','2020-04-16'),
('B','2020-05-03','2020-06-16'),
('C','2020-03-03','2020-03-10'),
('C','2020-04-03','2020-05-16'),
('C','2020-05-23','2020-08-10'),
('C','2020-09-23','2020-10-10')
;WITH CTE AS (
SELECT
username
, StartDate,enddate
, LEAD(StartDate) OVER (PARTITION BY USERNAME
ORDER BY StartDate ASC
) NextStartDate
FROM [user])
SELECT username,COUNT(*) COUNT FROM CTE
WHERE DATEDIFF(DD,enddate,NextStartDate)<=30
GROUP BY username
Output:
username COUNT
A 2
B 1
C 2
Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
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.