Time to be split in time slots...

  • Hello,

    I stumbled upon this today while doing reports and thought of asking some help; Here is the scenario

    We have a table which stores all the activities that are coming in for a particular day and are stored in the following format.User sends in time parameter for Morning,lunch,afternoon,evening. I have to report the number of minutes consumed by each department per slot of time.

    Each slot is defined by Morning to lunch; Lunch to Afternoon; Afternoon to Eve. and total time is defined as Morning to Evening. When i have a job which fits exactly into the criteria of one of the above slots, everything is fine and works out well with case statements. However when i have a job which spawns over two or more duration I want to split it based on the time occupied in each of those slots. Here is the sample data:

    DROP TABLE #Activity

    CREATE TABLE #Activity

    (

    ActivityId INT,

    DepartmentID INT,

    ActivityName NVARCHAR(100),

    ActivityStartDate DATETIME ,

    ActivityEndDate DATETIME,

    )

    INSERT INTO #Activity (ActivityId ,DepartmentID,

    ActivityName,

    ActivityStartDate ,

    ActivityEndDate )

    VALUES ( 1,201,'Initial Review', '2010-01-05 08:30:00.000', '2010-01-05 10:15:00.000'),

    ( 2,201,'Incoming Document', '2010-01-05 09:30:00.000', '2010-01-05 12:30:00.000'),

    ( 3,201,'Final Analysis', '2010-01-05 18:12:00.000', '2010-01-05 21:00:00.000'),

    ( 4,205,'Documentation', '2010-01-06 08:00:00.000', '2010-01-06 18:00:00.000'),

    ( 5,205,'Documentation', '2010-01-06 08:30:00.000', '2010-01-06 18:00:00.000'),

    ( 6,203,'Documentation', '2010-01-07 08:30:00.000', '2010-01-07 18:00:00.000'),

    ( 7,203,'Review and approve', '2010-01-07 14:00:00.000', '2010-01-07 15:00:00.000')

    DECLARE@morningStartTime NVARCHAR(10) = '06:10 AM',

    @lunchStartTime NVARCHAR(10) = '11:30 AM',

    @afternoonStartTime NVARCHAR(10)= '14:10 ',

    @eveningStartTime NVARCHAR(10)= '06:10 PM',

    @eveningEndTime NVARCHAR(10) = '09:35 PM'

    DECLARE @morningStartTimeActual TIME,

    @lunchStartTimeActual TIME ,

    @afternoonStartTimeActual TIME,

    @eveningStartTimeActual TIME,

    @eveningEndTimeActual TIME,

    @MorningTime INT,

    @lunchtime INT ,

    @AfternoonTime INT ,

    @EveningTime INT,

    @TotalTime INT

    SET @morningStartTimeActual = CAST(@morningStartTime AS TIME)

    SET @lunchStartTimeActual = CAST(@lunchStartTime AS TIME)

    SET @afternoonStartTimeActual = CAST(@afternoonStartTime AS TIME)

    SET @eveningStartTimeActual = CAST(@eveningStartTime AS TIME)

    SET @eveningEndTimeActual = CAST(@eveningEndTime AS TIME)

    SET @morningTime = DATEDIFF(mi,@morningStartTimeActual,@lunchStartTimeActual)

    SET @lunchtime = DATEDIFF(mi,@lunchStartTimeActual,@afternoonStartTimeActual)

    SET @afternoonTime = DATEDIFF(mi,@afternoonStartTimeActual,@eveningStartTimeActual)

    SET @eveningTime = DATEDIFF(mi,@eveningStartTimeActual,@eveningEndTimeActual)

    SET @totalTime = DATEDIFF (mi,@morningStartTime,@eveningEndTIme)

    SELECT @morningStartTimeActual AS morningStartTime,

    @lunchStartTimeActual AS lunchStartTime,

    @afternoonStartTimeActual AS afternoonStartTime,

    @eveningStartTimeActual AS eveningStartTime,

    @eveningEndTimeActual AS eveningEndTime,

    @morningTime AS Morning,

    @lunchtime AS Lunch,

    @afternoonTime AS Afternoon,

    @eveningTime AS Evening,

    @totalTime AS totalTime

    SELECT *,DATEDIFF(mi,ActivityStartDate,ActivityEndDate)AS JobTime,

    @morningTime AS morningTime,

    @lunchtime AS lunchTime ,

    @afternoonTime AS afternoonTime,

    @eveningTime AS eveningTime ,

    CASE WHEN CAST(ActivityStartDate AS TIME) >= @morningStartTimeActual AND CAST(ActivityStartDate AS TIME) <= @lunchStartTimeActual

    AND CAST(ActivityEndDate AS TIME) >= @morningStartTimeActual AND CAST(ActivityEndDate AS TIME) <= @lunchStartTimeActual

    THEN DATEDIFF(mi,ActivityStartDate,ActivityEndDate)

    WHEN CAST(ActivityStartDate AS TIME) BETWEEN @lunchStartTimeActual AND @afternoonStartTimeActual

    AND CAST(ActivityEndDate AS TIME) BETWEEN @lunchStartTimeActual AND @afternoonStartTimeActual

    THEN DATEDIFF(mi,ActivityStartDate,ActivityEndDate)

    WHEN CAST(ActivityStartDate AS TIME) BETWEEN @afternoonStartTimeActual AND @eveningStartTimeActual

    AND CAST(ActivityEndDate AS TIME) BETWEEN @afternoonStartTimeActual AND @eveningStartTimeActual

    THEN DATEDIFF(mi,ActivityStartDate,ActivityEndDate)

    WHEN CAST(ActivityStartDate AS TIME) BETWEEN @eveningStartTimeActual AND @eveningEndTimeActual

    AND CAST(ActivityEndDate AS TIME) BETWEEN @eveningStartTimeActual AND @eveningEndTimeActual

    THEN DATEDIFF(mi,ActivityStartDate,ActivityEndDate)

    END TotalMinutes

    FROM #Activity

    Ultimately the output I need is irrespective of the jobs but should show the number of minutes a department was working on jobs overall.

    o/p required is :

    DepartmentTime used

    ==================================================================

    201 Morning (06:10 to 11:30) : 225 ( 8.30 to 10.15 = 105 min + 9.30 to 11.30 used 120 minutes , since the time slot is only till 06.10 to 11.30)

    201 Lunch : (11:30 to14:10)60 ( 9.30 to 12.30 = 180 min ; However Lunch is only between 11.30 to 14.10 hence it just used 60 min of the lunch slot )

    201 Afternoon (14:10 to 18.10) :0 ( )

    201 Evening (18.10 to 21.35) : 168 ( )

  • Duplicate post

    Please post answers here

    http://www.sqlservercentral.com/Forums/Topic1239489-392-1.aspx

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • SQLRNNR (1/20/2012)


    Duplicate post

    Please post answers here

    http://www.sqlservercentral.com/Forums/Topic1239489-392-1.aspx

    Confusingly, the linked thread refers to itself as a duplicate. I think we should answer here :unsure:

  • SQL Kiwi (1/20/2012)


    SQLRNNR (1/20/2012)


    Duplicate post

    Please post answers here

    http://www.sqlservercentral.com/Forums/Topic1239489-392-1.aspx

    Confusingly, the linked thread refers to itself as a duplicate. I think we should answer here :unsure:

    I saw that after I posted. OP updated at ~ same time as I posted.

    SSC version of a Loop

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Sorry about the confusion. I wish I had the rights to take down a POST ( of course posted by Me)!

  • ulteriorm (1/20/2012)


    Sorry about the confusion. I wish I had the rights to take down a POST ( of course posted by Me)!

    NP - I am looking at your query but it will be a bit

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • The only way I was able to solve the problem is to analyze each and every case in the following scenario and use CASE statements to calculate the minutes See attachment:

    However this doesnot seem to be the right approach to the problem; Anyone with any insights would be appreciated.

  • I'm sure there's a better way, but this should be what you're after -

    BEGIN TRAN

    CREATE TABLE #Activity(ActivityId INT,DepartmentID INT,ActivityName NVARCHAR(100),ActivityStartDate DATETIME,ActivityEndDate DATETIME)

    INSERT INTO #Activity (ActivityId ,DepartmentID,ActivityName,ActivityStartDate,ActivityEndDate)

    VALUES ( 1,201,'Initial Review', '2010-01-05 08:30:00.000', '2010-01-05 10:15:00.000'),

    ( 2,201,'Incoming Document', '2010-01-05 09:30:00.000', '2010-01-05 12:30:00.000'),

    ( 3,201,'Final Analysis', '2010-01-05 18:12:00.000', '2010-01-05 21:00:00.000'),

    ( 4,205,'Documentation', '2010-01-06 08:00:00.000', '2010-01-06 18:00:00.000'),

    ( 5,205,'Documentation', '2010-01-06 08:30:00.000', '2010-01-06 18:00:00.000'),

    ( 6,203,'Documentation', '2010-01-07 08:30:00.000', '2010-01-07 18:00:00.000'),

    ( 7,203,'Review and approve', '2010-01-07 14:00:00.000', '2010-01-07 15:00:00.000');

    DECLARE@morningStartTime TIME = '06:10', @lunchStartTime TIME = '11:30',

    @afternoonStartTime TIME = '14:10', @eveningStartTime TIME = '18:10',

    @eveningEndTime TIME = '21:35';

    WITH t1(n) AS (SELECT 1 UNION ALL SELECT 1),

    t2(n) AS (SELECT 1 FROM t1 x, t1 y),

    t3(n) AS (SELECT 1 FROM t2 x, t2 y),

    t4(n) AS (SELECT 1 FROM t3 x, t3 y),

    tally(n) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))

    FROM t4 x, t4 y),

    morning(n) AS (SELECT CAST(DATEADD(MI,n-1,@morningStartTime) AS TIME)

    FROM tally

    WHERE n <= DATEDIFF(MI,@morningStartTime,@lunchStartTime)),

    lunch(n) AS (SELECT CAST(DATEADD(MI,n-1,@lunchStartTime) AS TIME)

    FROM tally

    WHERE n <= DATEDIFF(MI,@lunchStartTime,@afternoonStartTime)),

    afternoon(n) AS (SELECT CAST(DATEADD(MI,n-1,@afternoonStartTime) AS TIME)

    FROM tally

    WHERE n <= DATEDIFF(MI,@afternoonStartTime,@eveningStartTime)),

    evening(n) AS (SELECT CAST(DATEADD(MI,n-1,@eveningStartTime) AS TIME)

    FROM tally

    WHERE n <= DATEDIFF(MI,@eveningStartTime,@eveningEndTime))

    SELECT ActivityId, DepartmentID, morningMinutes, lunchMinutes, afternoonMinutes, eveningMinutes

    FROM #Activity

    CROSS APPLY (SELECT COUNT(n) AS morningMinutes

    FROM morning sp

    WHERE sp.n BETWEEN CAST(ActivityStartDate AS TIME) AND CAST(ActivityEndDate AS TIME)) morning

    CROSS APPLY (SELECT COUNT(n) AS lunchMinutes

    FROM lunch sp

    WHERE sp.n BETWEEN CAST(ActivityStartDate AS TIME) AND CAST(ActivityEndDate AS TIME)) lunch

    CROSS APPLY (SELECT COUNT(n) AS afternoonMinutes

    FROM afternoon sp

    WHERE sp.n BETWEEN CAST(ActivityStartDate AS TIME) AND CAST(ActivityEndDate AS TIME)) afternoon

    CROSS APPLY (SELECT COUNT(n) AS eveningMinutes

    FROM evening sp

    WHERE sp.n BETWEEN CAST(ActivityStartDate AS TIME) AND CAST(ActivityEndDate AS TIME)) evening;

    ROLLBACK

    Returns the following minutes: -

    ActivityId DepartmentID morningMinutes lunchMinutes afternoonMinutes eveningMinutes

    ----------- ------------ -------------- ------------ ---------------- --------------

    1 201 106 0 0 0

    2 201 120 61 0 0

    3 201 0 0 0 169

    4 205 210 160 231 0

    5 205 180 160 231 0

    6 203 180 160 231 0

    7 203 0 10 51 0

    Blergh! Bug fix 😀

    BEGIN TRAN

    CREATE TABLE #Activity(ActivityId INT,DepartmentID INT,ActivityName NVARCHAR(100),ActivityStartDate DATETIME,ActivityEndDate DATETIME)

    INSERT INTO #Activity (ActivityId ,DepartmentID,ActivityName,ActivityStartDate,ActivityEndDate)

    VALUES ( 1,201,'Initial Review', '2010-01-05 08:30:00.000', '2010-01-05 10:15:00.000'),

    ( 2,201,'Incoming Document', '2010-01-05 09:30:00.000', '2010-01-05 12:30:00.000'),

    ( 3,201,'Final Analysis', '2010-01-05 18:12:00.000', '2010-01-05 21:00:00.000'),

    ( 4,205,'Documentation', '2010-01-06 08:00:00.000', '2010-01-06 18:00:00.000'),

    ( 5,205,'Documentation', '2010-01-06 08:30:00.000', '2010-01-06 18:00:00.000'),

    ( 6,203,'Documentation', '2010-01-07 08:30:00.000', '2010-01-07 18:00:00.000'),

    ( 7,203,'Review and approve', '2010-01-07 14:00:00.000', '2010-01-07 15:00:00.000'),

    ( 8,203,'Review and approve', '2010-01-07 06:10:00.000', '2010-01-07 11:30:00.000');

    DECLARE@morningStartTime TIME = '06:10', @lunchStartTime TIME = '11:30',

    @afternoonStartTime TIME = '14:10', @eveningStartTime TIME = '18:10',

    @eveningEndTime TIME = '21:35';

    WITH t1(n) AS (SELECT 1 UNION ALL SELECT 1),

    t2(n) AS (SELECT 1 FROM t1 x, t1 y),

    t3(n) AS (SELECT 1 FROM t2 x, t2 y),

    t4(n) AS (SELECT 1 FROM t3 x, t3 y),

    tally(n) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))

    FROM t4 x, t4 y),

    morning(n) AS (SELECT CAST(DATEADD(MI,n-1,@morningStartTime) AS TIME)

    FROM tally

    WHERE n <= DATEDIFF(MI,@morningStartTime,@lunchStartTime)),

    lunch(n) AS (SELECT CAST(DATEADD(MI,n-1,@lunchStartTime) AS TIME)

    FROM tally

    WHERE n <= DATEDIFF(MI,@lunchStartTime,@afternoonStartTime)),

    afternoon(n) AS (SELECT CAST(DATEADD(MI,n-1,@afternoonStartTime) AS TIME)

    FROM tally

    WHERE n <= DATEDIFF(MI,@afternoonStartTime,@eveningStartTime)),

    evening(n) AS (SELECT CAST(DATEADD(MI,n-1,@eveningStartTime) AS TIME)

    FROM tally

    WHERE n <= DATEDIFF(MI,@eveningStartTime,@eveningEndTime))

    SELECT ActivityId, DepartmentID, morningMinutes, lunchMinutes, afternoonMinutes, eveningMinutes

    FROM #Activity

    CROSS APPLY (SELECT COUNT(n) AS morningMinutes

    FROM morning sp

    WHERE sp.n >= CAST(ActivityStartDate AS TIME) AND sp.n < CAST(ActivityEndDate AS TIME)) morning

    CROSS APPLY (SELECT COUNT(n) AS lunchMinutes

    FROM lunch sp

    WHERE sp.n >= CAST(ActivityStartDate AS TIME) AND sp.n < CAST(ActivityEndDate AS TIME)) lunch

    CROSS APPLY (SELECT COUNT(n) AS afternoonMinutes

    FROM afternoon sp

    WHERE sp.n >= CAST(ActivityStartDate AS TIME) AND sp.n < CAST(ActivityEndDate AS TIME)) afternoon

    CROSS APPLY (SELECT COUNT(n) AS eveningMinutes

    FROM evening sp

    WHERE sp.n >= CAST(ActivityStartDate AS TIME) AND sp.n < CAST(ActivityEndDate AS TIME)) evening;

    ROLLBACK

    Returns: -

    ActivityId DepartmentID morningMinutes lunchMinutes afternoonMinutes eveningMinutes

    ----------- ------------ -------------- ------------ ---------------- --------------

    1 201 105 0 0 0

    2 201 120 60 0 0

    3 201 0 0 0 168

    4 205 210 160 230 0

    5 205 180 160 230 0

    6 203 180 160 230 0

    7 203 0 10 50 0

    8 203 320 0 0 0

    I guess my issue was lack of sample data, as soon as I added that 8th row I could see exactly what I'd overlooked. Which is the reason I normally test with a small data-set followed by medium, then large and finally a data-set that is larger than any I ever expect to need to test against.

    Anyway, it's not a perfect solution but should be adequate for your requirements.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • DROP TABLE #Activity

    CREATE TABLE #Activity

    (

    ActivityId integer NOT NULL,

    DepartmentID integer NOT NULL,

    ActivityName nvarchar(100) NOT NULL,

    ActivityStartDate datetime NOT NULL,

    ActivityEndDate datetime NOT NULL,

    )

    INSERT #Activity

    (ActivityId, DepartmentID, ActivityName, ActivityStartDate, ActivityEndDate)

    VALUES

    ( 1,201,'Initial Review', '2010-01-05 08:30:00.000', '2010-01-05 10:15:00.000'),

    ( 2,201,'Incoming Document', '2010-01-05 09:30:00.000', '2010-01-05 12:30:00.000'),

    ( 3,201,'Final Analysis', '2010-01-05 18:12:00.000', '2010-01-05 21:00:00.000'),

    ( 4,205,'Documentation', '2010-01-06 08:00:00.000', '2010-01-06 18:00:00.000'),

    ( 5,205,'Documentation', '2010-01-06 08:30:00.000', '2010-01-06 18:00:00.000'),

    ( 6,203,'Documentation', '2010-01-07 08:30:00.000', '2010-01-07 18:00:00.000'),

    ( 7,203,'Review and approve', '2010-01-07 14:00:00.000', '2010-01-07 15:00:00.000')

    DECLARE @Ranges TABLE

    (

    RangeName nvarchar(10) PRIMARY KEY,

    StartTime time NOT NULL,

    EndTime time NOT NULL,

    CHECK (EndTime > StartTime)

    )

    INSERT @Ranges

    (RangeName, StartTime, EndTime)

    VALUES

    (N'Morning', '06:10', '11:30'),

    (N'Lunch', '11:30', '14:10'),

    (N'Afternoon', '14:10', '18:10'),

    (N'Evening', '18:10', '21:35');

    SELECT

    a.DepartmentID,

    r.RangeName,

    SUM(IntervalUsed.in_minutes)

    FROM #Activity AS a

    CROSS APPLY

    (

    -- Just the time portion needed

    SELECT

    StartTime = CONVERT(time, a.ActivityStartDate),

    EndTime = CONVERT(time, a.ActivityEndDate)

    ) AS TimeOnly

    -- Join to defined ranges that overlap

    JOIN @Ranges AS r ON

    TimeOnly.StartTime < r.EndTime

    AND TimeOnly.EndTime >= r.StartTime

    CROSS APPLY

    (

    -- Choose either the range or activity

    -- start and end time for each overlap

    SELECT

    StartTime =

    CASE

    WHEN r.StartTime <= TimeOnly.StartTime

    THEN TimeOnly.StartTime

    ELSE r.StartTime

    END,

    EndTime =

    CASE

    WHEN r.EndTime >= TimeOnly.EndTime

    THEN TimeOnly.EndTime

    ELSE r.EndTime

    END

    ) AS Overlap

    CROSS APPLY

    (

    -- Difference in minutes for each overlap

    SELECT

    DATEDIFF(MINUTE, Overlap.StartTime, Overlap.EndTime)

    ) AS IntervalUsed (in_minutes)

    GROUP BY

    a.DepartmentID,

    r.RangeName

    ORDER BY

    a.DepartmentID,

    MAX(r.StartTime);

  • Paul's solution is probably better, but I hadn't realised we were meant to be totalling the minutes per department.

    BEGIN TRAN

    CREATE TABLE #Activity(ActivityId INT,DepartmentID INT,ActivityName NVARCHAR(100),ActivityStartDate DATETIME,ActivityEndDate DATETIME)

    INSERT INTO #Activity (ActivityId ,DepartmentID,ActivityName,ActivityStartDate,ActivityEndDate)

    VALUES ( 1,201,'Initial Review', '2010-01-05 08:30:00.000', '2010-01-05 10:15:00.000'),

    ( 2,201,'Incoming Document', '2010-01-05 09:30:00.000', '2010-01-05 12:30:00.000'),

    ( 3,201,'Final Analysis', '2010-01-05 18:12:00.000', '2010-01-05 21:00:00.000'),

    ( 4,205,'Documentation', '2010-01-06 08:00:00.000', '2010-01-06 18:00:00.000'),

    ( 5,205,'Documentation', '2010-01-06 08:30:00.000', '2010-01-06 18:00:00.000'),

    ( 6,203,'Documentation', '2010-01-07 08:30:00.000', '2010-01-07 18:00:00.000'),

    ( 7,203,'Review and approve', '2010-01-07 14:00:00.000', '2010-01-07 15:00:00.000'),

    ( 8,203,'Review and approve', '2010-01-07 06:10:00.000', '2010-01-07 11:30:00.000');

    DECLARE@morningStartTime TIME = '06:10', @lunchStartTime TIME = '11:30',

    @afternoonStartTime TIME = '14:10', @eveningStartTime TIME = '18:10',

    @eveningEndTime TIME = '21:35';

    WITH t1(n) AS (SELECT 1 UNION ALL SELECT 1),

    t2(n) AS (SELECT 1 FROM t1 x, t1 y),

    t3(n) AS (SELECT 1 FROM t2 x, t2 y),

    t4(n) AS (SELECT 1 FROM t3 x, t3 y),

    tally(n) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))

    FROM t4 x, t4 y),

    morning(n) AS (SELECT CAST(DATEADD(MI,n-1,@morningStartTime) AS TIME)

    FROM tally

    WHERE n <= DATEDIFF(MI,@morningStartTime,@lunchStartTime)),

    lunch(n) AS (SELECT CAST(DATEADD(MI,n-1,@lunchStartTime) AS TIME)

    FROM tally

    WHERE n <= DATEDIFF(MI,@lunchStartTime,@afternoonStartTime)),

    afternoon(n) AS (SELECT CAST(DATEADD(MI,n-1,@afternoonStartTime) AS TIME)

    FROM tally

    WHERE n <= DATEDIFF(MI,@afternoonStartTime,@eveningStartTime)),

    evening(n) AS (SELECT CAST(DATEADD(MI,n-1,@eveningStartTime) AS TIME)

    FROM tally

    WHERE n <= DATEDIFF(MI,@eveningStartTime,@eveningEndTime))

    SELECT DepartmentID, SUM(morningMinutes) AS morningMinutes,

    SUM(lunchMinutes) AS lunchMinutes, SUM(afternoonMinutes) AS afternoonMinutes,

    SUM(eveningMinutes) AS eveningMinutes

    FROM #Activity

    CROSS APPLY (SELECT COUNT(n) AS morningMinutes

    FROM morning sp

    WHERE sp.n >= CAST(ActivityStartDate AS TIME) AND sp.n < CAST(ActivityEndDate AS TIME)) morning

    CROSS APPLY (SELECT COUNT(n) AS lunchMinutes

    FROM lunch sp

    WHERE sp.n >= CAST(ActivityStartDate AS TIME) AND sp.n < CAST(ActivityEndDate AS TIME)) lunch

    CROSS APPLY (SELECT COUNT(n) AS afternoonMinutes

    FROM afternoon sp

    WHERE sp.n >= CAST(ActivityStartDate AS TIME) AND sp.n < CAST(ActivityEndDate AS TIME)) afternoon

    CROSS APPLY (SELECT COUNT(n) AS eveningMinutes

    FROM evening sp

    WHERE sp.n >= CAST(ActivityStartDate AS TIME) AND sp.n < CAST(ActivityEndDate AS TIME)) evening

    GROUP BY DepartmentID;

    ROLLBACK

    DepartmentID morningMinutes lunchMinutes afternoonMinutes eveningMinutes

    ------------ -------------- ------------ ---------------- --------------

    201 225 60 0 168

    203 500 170 280 0

    205 390 320 460 0


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Both the solutions work; Thanks for the help,

    @cadavre: It does not matter how you have represented the solution in each individual groups. i have more work to do on the final report which has its other complexities (which i have figured out already)

    And you were right paul's solution is more elegant ofcourse!

    Thanks..

  • Cadavre (1/24/2012)


    Paul's solution is probably better...

    Different; better is always subjective 🙂

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply