How to merge time intervals in SQL Server

前端 未结 7 1764
再見小時候
再見小時候 2021-01-03 01:43

Suppose I have the following an event table with personId, startDate and endDate.

I want to know how much time the person X sp

7条回答
  •  心在旅途
    2021-01-03 02:13

    Algebra. If B-n is the ending time of the nth event, and A-n is the starting time of the nth event, then the sum of the differences is the difference of the sums. So you can write

    select everything else, sum(cast(endDate as int)) - sum(cast(startDate as int)) as daysSpent
    

    If your dates have no time component, this works. Otherwise, you could use a real.

提交回复
热议问题