SQL Query to get output based on date month, if month are consecutive output should be consecutive number starting from 1, if not then starting from 1 ?


How to get output based on date month, if date month is consecutive then output should be consecutive number starting from 1. If date month is not consecutive like (Jan, March), then output should start from 1 for non consecutive month 

Date Output
Dec 14  1
Jan 15 2
Feb 15 3
Mrch 15  4
June 15 1
July 15 2
Aug 15  3







August 31st, 2015 3:12am

SELECT dt, ROW_NUMBER() OVER(PARTITION BY grp ORDER BY dt) output1
FROM
(
SELECT *,
 DATEADD(MONTH, -1 * DENSE_RANK() OVER(ORDER BY dt), dt) AS grp  from #t
 ) AS Der
Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 3:27am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics