• m.dunster (10/30/2013)


    Hi,

    Wondering if anyone has an elegant solution to finding out how much time has elapsed for a specific hour.

    declare @t as table (ptr int identity, START time, ENDD time)

    insert into @t (START, ENDD)

    SELECT '09:30', '10:30'

    union all

    SELECT '10:00', '11:00'

    union all

    SELECT '10:30', '11:30'

    Taking the example table above, I am looking to output the amount of time elapsed for each hour block. So:

    09 - 0.5

    10 - 2

    11 - 0.5

    I can think of complicated and ugly ways using cursors and many lines of code, but I thought someone might have a more user friendly solution!

    Many thanks for your time.

    Matt

    Thank you for providing ddl and sample data. I am confused by your expected output. Can you try to explain what you want? The business rules for the output is lost on me.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/