Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Come faccio a raggruppare su intervalli continui

WITH    q AS
        (
        SELECT  *,
                ROW_NUMBER() OVER (PARTITION BY crew, dayType ORDER BY [date]) AS rnd,
                ROW_NUMBER() OVER (PARTITION BY crew ORDER BY [date]) AS rn
        FROM    mytable
        )
SELECT  MIN([date]), MAX([date]), crew AS name, dayType
FROM    q
GROUP BY
        crew, dayType, rnd - rn

Questo articolo potrebbe interessarti:

  • Cose di cui SQL ha bisogno:SERIES()