简体   繁体   English

行之间的数学比较

[英]Mathematical comparison between rows

I have a table 我有桌子

+----+-----------+---------+
| ID | StartTime | EndTime |
+----+-----------+---------+
|  1 | 2:00pm    | 3:00pm  |
|  2 | 4:00pm    | 5:00pm  |
|  3 | 7:00pm    | 9:00pm  |
+----+-----------+---------+

I need to get the difference between the end time of one row and the start time of the NEXT row. 我需要获得一行的结束时间和NEXT行的开始时间之间的差。 ie End time of row 1 compared to start time of row 2, or end time of row 2 compared to start time of row 3. 即第1行的结束时间与第2行的开始时间相比,或第2行的结束时间与第3行的开始时间相比。

Ideally I'd like a result that looks similar to 理想情况下,我希望得到的结果类似于

+----+----------------+
| ID | TimeDifference |
+----+----------------+
|  2 | 1.0 hours      |
|  3 | 2.0 hours      |
+----+----------------+

I have no clue whatsoever on how to do something like this. 我完全不知道如何做这样的事情。 I'm thinking that I may need 2 temp tables, one to hold start times another for end times so that I can more easily do the comparisons, but honestly that's just a shot in the dark at the moment. 我在想可能需要2个临时表,一个临时表用于保存开始时间,另一个临时表用于结束时间,这样我就可以更轻松地进行比较,但是说实话,这只是目前的黑暗。

FYI, on server 2008 in case that makes a difference for some of the commands. 仅供参考,在服务器2008上,以防某些命令有所不同。

NOTE: The question was not tagged SQL Server 2008 when this answer was written. 注意:编写此答案时,该问题未标记为SQL Server 2008。

You can use lag() : 您可以使用lag()

select t.*,
       datediff(minute, lag(endtime) over (order by id), starttime) / 60.0 as hours_diff
from t;

This does not filter out any rows. 这不会过滤掉任何行。 The description of the problem ("next row") and the sample data (which is based on "previous row") are inconsistent. 问题的描述(“下一行”)和示例数据(基于“上一行”)不一致。

Well, since it's 2008 version you can't use the Lead() or Lag() window functions, but you can use subqueries to mimic them: 好吧,因为它是2008版本,所以您不能使用Lead()Lag()窗口函数,但是可以使用子查询来模仿它们:

SELECT Id, 
       DATEDIFF(minute,
            (
                SELECT TOP 1 EndTime
                FROM table t1
                WHERE t1.Id < t0.Id 
                ORDER BY t1.Id DESC
            ), StartTime) / 60.0 As TimeDifference 
FROM Table t0
WHERE EXISTS             
(
    SELECT 1 
    FROM Table t2
    WHERE t2.Id < t0.Id    
)

You can try it 你可以试试

declare @t as table (ID int,  StartTime time , EndTime time)
INSERT @t SELECT 1  ,'2:00pm',     '3:00pm'  
INSERT @t SELECT 2  ,'4:00pm',     '5:00pm'  
INSERT @t SELECT 3  ,'7:00pm',     '9:00pm'


---- For sequential IDs
select 
a.ID
,a.StartTime
,a.EndTime
,datediff(minute, (SELECT EndTime FROM @t b where b.ID = a.ID - 1) , a.StartTime) / 60.0 as hours_diff
from @t a 


---- For non-sequential IDs

;WIth cte_times as (
SELECT 
    ROW_NUMBER() OVER (ORDER BY Id) as new_ID
    , ID
    ,StartTime
    ,EndTime
FROM 
    @t
) 
select 
a.ID
,a.StartTime
,a.EndTime
,datediff(minute, (SELECT EndTime FROM cte_times b where b.new_ID = a.new_ID - 1) , a.StartTime) / 60.0 as hours_diff
from cte_times a 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM