从两个不同列的两个不同行上查找两个日期之间的时间

Finding time between two dates on two different rows from two different columns

您好,我希望你们能提供帮助,我加入了几个表格以获得我需要的正确信息列表。现在我被困在最后一件事上。 我需要在一行和一列中找到从末尾 date/time 开始的小时数,以及从不同列中不同行的开始 date/time 的小时数。

-------------------------------------------------
ID Name Startdate   EndDate
-------------------------------------------------
1 BILL 2017-10-10 09:00 2017-10-10 19:00
1 BILL 2017-10-11 09:00 2017-10-11 19:00
1 BILL 2017-10-15 09:00 2017-10-15 15:00
1 BILL 2017-10-22 09:00 2017-10-22 11:00
2 TOM 2017-10-10 09:00 2017-10-10 14:00
2 TOM 2017-10-12 09:00 2017-10-12 16:00
3 SAM 2017-10-13 09:00 2017-10-13 20:00
3 SAM 2017-10-14 09:00 2017-10-14 19:00
-------------------------------------------------

--------------------------------------------------------------------
ID Name Startdate   EndDate   Hours Diff
--------------------------------------------------------------------
1 BILL 2017-10-10 09:00 2017-10-10 19:00 NULL
1 BILL 2017-10-11 09:00 2017-10-11 19:00 14
1 BILL 2017-10-15 09:00 2017-10-15 15:00 86
1 BILL 2017-10-22 09:00 2017-10-22 11:00 162
2 TOM 2017-10-10 09:00 2017-10-10 14:00 NULL
2 TOM 2017-10-12 09:00 2017-10-12 16:00 43
3 SAM 2017-10-13 09:00 2017-10-13 20:00 NULL
3 SAM 2017-10-14 09:00 2017-10-14 19:00 13
--------------------------------------------------------------------

SELECT 
a.ID
,b.FirstName
,b.LastName
,c.StartTime
,c.EndTime

from
table1 as a
inner join table2 as b on a.idnumber = b.idnumber
left join table3 as c on a.idnumber = c.idnumber

where

((Select Count(idnumber) as expr1
From table1 as ab
where idnumber = a.idnumber))<=1)

Order by a.idnumber

在 SQL 中完成会很好,但如果在 SSRS 中用表达式而不是 SQL 更简单,因为这将被放入一个很棒的报告中。

任何帮助都会很棒,我相信遇到问题很简单。 谢谢

对于这个简单的案例,您可以使用:

select id, name, startdate, enddate,
   DATEDIFF(hh, (
     select MAX(enddate) 
     from Table1 as T2
     where T1.id = T2.id
     and   T1.startdate > T2.enddate ), startdate)
   as [Hours Diff]
from table1 as T1
order by id, startdate

这里有一个工作示例:http://sqlfiddle.com/#!6/95827/8

您要比较的行是否按顺序排列? Row2 结束日期与 Row1 开始日期 Row3 结束日期与 Row2 开始日期 等...

如果是这种情况,您可以使用 "lag" 函数将不同行的值引入到正在比较的当前行中。

我用 Turophile 的代码做了类似的事情:

select 
  id, 
  name, 
  startdate, 
  enddate,
datediff(hh,lag(startdate)over(partition by name order by startdate 
asc),enddate)

from 
  table1 as T1
order by id, startdate

使用LAG()-获取以前的记录数据,使用DATEDIFF-获取date/hour差值

SELECT  ID,Name ,Startdate,EndDate,
        DATEDIFF(HOUR,
                    LAG(EndDate) OVER(PARTITION BY ID ORDER BY Startdate),
                Startdate)  AS  Hours_Diff
FROM    TABLE1
ORDER BY ID,Startdate

结果:-

ID  Name    Startdate           EndDate             Hours_Diff

1   BILL    2017-10-10 09:00    2017-10-10 19:00    NULL
1   BILL    2017-10-11 09:00    2017-10-11 19:00    14
1   BILL    2017-10-15 09:00    2017-10-15 15:00    86
1   BILL    2017-10-22 09:00    2017-10-22 11:00    162
2   TOM     2017-10-10 09:00    2017-10-10 14:00    NULL
2   TOM     2017-10-12 09:00    2017-10-12 16:00    43
3   SAM     2017-10-13 09:00    2017-10-13 20:00    NULL
3   SAM     2017-10-14 09:00    2017-10-14 19:00    13