Home Forums SQL Server 2008 T-SQL (SS2K8) consecutive days count irrespective of weekends, holidays RE: consecutive days count irrespective of weekends, holidays

  • SELECT

    Studentid,

    MIN(dateid) AS First_Absence_Date,

    MAX(dateid) AS Last_Absence_Date,

    COUNT(*) AS Total_Consecutive_Absences

    FROM (

    SELECT

    Studentid, attendanceind, dateid,

    ROW_NUMBER() OVER (PARTITION BY Studentid ORDER BY dateid) -

    ROW_NUMBER() OVER (PARTITION BY Studentid, attendanceind ORDER BY dateid) AS Grp#

    FROM #x x --<<-- change to your table name

    ) AS derived

    WHERE

    attendanceind = 0

    GROUP BY

    Studentid, grp#

    HAVING

    COUNT(*) >= 15

    ORDER BY

    Studentid, First_Absence_Date

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.