Calculate the sum of duration ignoring overlapps

  • I have used the below query to calculate the sum of duration ignoring overlapps, However this does not take in to account the time in date-time format ie.. For calculations between 23:59 and 00:01 hours, Any suggestion on how this can be extended to be bug free in case of date change overs ?

    DECLARE @Sample TABLE (StartTime DATETIME, EndTime DATETIME)

    INSERT@Sample

    SELECT'07:12', '07:17' UNION ALL--00:05

    SELECT'07:18', '07:24' UNION ALL--00:06

    SELECT'07:33', '07:34' UNION ALL--00:01

    SELECT'07:34', '08:02' UNION ALL--00:28

    SELECT'07:34', '07:43' UNION ALL--00:00

    SELECT'07:43', '07:49' UNION ALL--00:00

    SELECT'07:53', '08:01' UNION ALL--00:00

    SELECT'07:57', '08:10'--00:06

    SELECTCOUNT(DISTINCT v.Number) AS Minutes

    FROMmaster..spt_values AS v

    INNER JOIN(

    SELECTDATEDIFF(MINUTE, '00:00:00', StartTime) AS StartMinute,

    DATEDIFF(MINUTE, '00:00:00', EndTime) AS EndMinute

    FROM@Sample

    ) AS x ON x.StartMinute <= v.Number

    AND x.EndMinute > v.Number

    WHEREv.Type = 'p'

    AND Number < 1440

    The result is 48 minutes.

  • - did you try just adding the correct date to both columns ?

    - and then just query

    datediff(MINUTE , startdate, enddate) as elapsttime_Minutes

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Great.

    You start the topic over here

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=101129&SearchTerms=DISTINCT,vNumber

    and then you continue here at SQLServerCentral?


    N 56°04'39.16"
    E 12°55'05.25"

  • Hi peter ,

    Sorry I did not receive a reply back from you.

    am in desperate need to get back home finishing my work hence this try....

Viewing 4 posts - 1 through 4 (of 4 total)

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