sql - Query Results For Consecutive Months In Column Grouped By Value -
the following sample data:
name | hours | rdate | company | ------------------------------------ |0 |2014-08-01 |w |0 |2014-07-01 |w |0 |2014-06-01 |w |0 |2014-05-01 |w b |0 |2014-08-01 |x c |0 |2014-07-01 |y c |0 |2014-06-01 |y d |0 |2014-08-01 |v d |0 |2014-07-01 |z
the following results desire:
name | hours | rdate | company | ------------------------------------ |0 |2014-08-01 |w |0 |2014-07-01 |w |0 |2014-06-01 |w |0 |2014-05-01 |w c |0 |2014-07-01 |y c |0 |2014-06-01 |y
so question is:
how results of rdate
consecutive months in columns i.e 2014-08-01, 2014-07-01
(2014-08-01, 2014-06-01
not satisfy)for same name
, same company
i'm thinking variation of grouping islands of contiguous dates problem.
;with cte as( select *, rn = dateadd(month, - row_number() on (partition name, company order rdate), rdate) test ) ,ctecount as( select *, cc = count(*) over(partition name, company, rn) cte ) select name, hours, rdate, company ctecount cc > 1
Comments
Post a Comment