我不确定我是否过于复杂这么小的任务,但是如果有人能帮助我一点正确的方向,那么我将不胜感激。
我有两个具有相同字段的表,我创建了一个包含这两个值的视图。
正如我所说的那样:
SELECT TOP 1000 [Name]
,[WorkOrder]
,[OnSiteStarted]
,[OnSiteSubmitted]
FROM [DBName].[dbo].[sw_onsite_offsite]
ORDER BY WorkOrder
结果集显示为(片段):
Name WorkOrder OnSiteStarted OnSiteSubmitted
On-Site 23022017 2017-02-23 20:31:49.000 2017-02-23 20:33:47.000
Off-Site 35 2017-02-15 18:32:26.000 2017-02-15 21:11:04.000
On-Site 64169086 2017-02-22 11:42:40.000 2017-02-22 15:10:17.000
Off-Site 91487689 2017-02-11 13:48:11.000 2017-02-11 14:32:37.000
On-Site 91494707 2017-02-13 14:39:42.000 2017-02-13 14:49:03.000
Off-Site 92810031 2017-02-17 09:30:59.000 2017-02-17 09:58:19.000
On-Site 92810031 2017-02-17 09:00:12.000 2017-02-17 09:30:48.000
Off-Site 92810034 2017-02-17 10:01:27.000 2017-02-17 10:24:45.000
On-Site 92810039 2017-03-03 12:33:41.000 2017-03-03 12:52:37.000
Off-Site 92810039 2017-03-03 12:44:52.000 2017-03-03 13:41:35.000
我所追求的最终结果是:当场外和现场表格具有相同的WorkOrder编号时,这将仅显示它们与DATEDIFF(MINUTES,[onsitesubmitted],[offsitesubmitted]字段。
CASE声明会以某种方式适用于此,还是其他的?
答案 0 :(得分:1)
我想你想要一个自我加入:
select t1.WorkOrder,
DATEDIFF(MINUTES, t1.onsitesubmitted, t2.OnSiteSubmitted) diff
from [DBName].[dbo].[sw_onsite_offsite] t1
join [DBName].[dbo].[sw_onsite_offsite] t2 on t1.WorkOrder = t2.WorkOrder
where t1.name = 'On-Site'
and t2.name = 'Off-Site'
这只返回那些异地和现场都有相同WorkOrder的行。如果您希望它在不匹配的情况下产生结果,则可以使用适当的外连接。
答案 1 :(得分:1)
.catch((response: Response) => {
return Observable.of(response);
});
答案 2 :(得分:1)
如果您的数据库支持它,您可以使用inspect.signature
或lead()
窗口函数(您没有标记数据库,但它看起来像sql-server)
lag()
sql server中的rextester演示:http://rextester.com/OTNS68646
返回:
select
Name
, WorkOrder
, OnSiteStarted
, OnSiteSubmitted
, datediff(minute
, lead([onsitesubmitted]) over (
partition by WorkOrder
order by Name
)
, [onsitesubmitted]
) as Duration
from [dbo].[sw_onsite_offsite]
order by WorkOrder, OnSiteSubmitted