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

  • Using Alan.B's set up data, here's another way (except using DATE data type):

    -- Setup

    IF OBJECT_ID('tempdb..#x') IS NOT NULL

    DROP TABLE #x;

    CREATE TABLE #x

    ( Studentid int, DateID DATE unique, attendaceind bit)

    INSERT INTO #x

    SELECT 1234, '20121031', 1 UNION ALL

    SELECT 1234, '20121102', 1 UNION ALL

    SELECT 1234, '20121101', 1 UNION ALL

    SELECT 1234, '20121105', 0 UNION ALL

    SELECT 1234, '20121107', 1 UNION ALL

    SELECT 1234, '20121108', 1 UNION ALL

    SELECT 1234, '20121109', 0 UNION ALL

    SELECT 1234, '20121113', 0 UNION ALL

    SELECT 1234, '20121115', 0 UNION ALL

    SELECT 1234, '20121114', 0 UNION ALL

    SELECT 1234, '20121116', 0 UNION ALL

    SELECT 1234, '20121119', 0 UNION ALL

    SELECT 1234, '20121120', 0 UNION ALL

    SELECT 1234, '20121126', 0 UNION ALL

    SELECT 1234, '20121127', 0 UNION ALL

    SELECT 1234, '20121128', 0 UNION ALL

    SELECT 1234, '20121129', 0 UNION ALL

    SELECT 1234, '20121130', 0 UNION ALL

    SELECT 1234, '20121203', 0 UNION ALL

    SELECT 1234, '20121204', 0 UNION ALL

    SELECT 1234, '20121205', 0 UNION ALL

    SELECT 1234, '20121206', 0 UNION ALL

    SELECT 1234, '20121207', 0 UNION ALL

    SELECT 1234, '20121210', 0 UNION ALL

    SELECT 1234, '20121211', 0 UNION ALL

    SELECT 1234, '20121212', 0 UNION ALL

    SELECT 1234, '20121213', 0 UNION ALL

    SELECT 1234, '20121214', 1 UNION ALL

    SELECT 1234, '20121217', 1 UNION ALL

    SELECT 1234, '20121218', 1 UNION ALL

    SELECT 1234, '20121219', 1

    GO

    DECLARE @startdate DATE='20121114',

    @endDate DATE='20121213';

    --;WITH Absences AS (

    SELECT StudentID, StartDT=MIN(DateID), EndDT=MAX(DateID)

    ,Absences=COUNT(1-attendaceind)

    FROM (

    SELECT StudentID, DateID, attendaceind

    ,n=DATEDIFF(day, DateID

    ,ROW_NUMBER() OVER (PARTITION BY StudentID ORDER BY DateID))

    FROM #x

    WHERE attendaceind = 0) a

    GROUP BY StudentID, n

    -- )

    --SELECT StudentID, StartDT=MIN(StartDT), EndDT=MAX(EndDT)

    -- ,Absences=SUM(Absences)

    --FROM (

    -- SELECT StudentID, StartDT, EndDT

    -- ,Absences=CASE WHEN StartDT >= @startdate AND EndDT <= @enddate THEN Absences

    -- WHEN StartDT < @startdate AND EndDT <= @enddate

    -- THEN DATEDIFF(day, @startdate, EndDT)

    -- WHEN StartDT >= @startdate AND EndDT > @enddate

    -- THEN DATEDIFF(day, StartDT, @enddate)

    -- ELSE DATEDIFF(day, @startdate, @enddate) END

    -- FROM Absences

    -- WHERE EndDT >= @startdate AND StartDT <= @enddate) a

    --GROUP BY StudentID

    DROP TABLE #x

    The initial code provided groups consecutive absences. If you uncomment the rest of the code it then groups again within the provided start and end dates summing up the absences that occur as part of a consecutive group of absences.

    When you run the first set of code, notice how one record starts at 11-13? The start date range of 11-14 requires the absence count to be adjusted in the final grouping and that is done with the CASE statement.

    All that's left is to add a HAVING clause to filter out only records appearing that are greater whatever your threshold is.

    I'm not sure which of the provided solutions will work best for you but surely there's something here you can work with.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St