我已经查询了:
SELECT
MemberId,
FirstName,LastName,
[DateOfChange]
,(select title from StatusList where idStatus=[OldStatus]) as [OldStatus]
,(select title from StatusList where idStatus=[NewStatus]) as [NewStatus]
FROM [statusLog] , Users where
statusLog.IdUser=Users.IdUser
此查询为我提供了以下结果:
我将OldStatus和NewStatus以及状态的DateOfChange作为列。
我只是希望在状态变化时间方面有所不同。
即。从DateOfChange For Old Status OnCall To Patrol我想找到两个日期之间的差异:
2014-04-04 16:13:33:000 and 2014-04-04 16:13:44:000
我试过:
SELECT
MemberId,
FirstName,LastName,
[DateOfChange],
DATEDIFF(HOUR,select [DateOfChange] from statusLog,Users where idstatus=[OldStatus]
,select [DateOfChange] from statusLog,Users where idstatus=[NewStatus])
,(select title from StatusList where idStatus=[OldStatus]) as [OldStatus]
,(select title from StatusList where idStatus=[NewStatus]) as [NewStatus]
FROM [statusLog] , Users where
statusLog.IdUser=Users.IdUser
但这不起作用。
我加入的两个表是:
用户:
statusLog:
请帮帮我。
如何才能在上面的查询中有这么几小时的差异?
修改
SELECT
MemberId,
FirstName,LastName,
[DateOfChange] ,
(SELECT
DATEDIFF(HOUR, SL.DateOfChange, SLN.StatusTo) AS StatusDuration
FROM
StatusLog SL
OUTER APPLY (
SELECT TOP(1)
DateOfChange AS StatusTo
FROM
StatusLog SLT
WHERE
SL.IdUser = SLT.IdUser
AND SLT.DateOfChange > SL.DateOfChange
ORDER BY
SLT.DateOfChange ASC
) SLN) Hourss
,(select title from StatusList where idStatus=[OldStatus]) as [OldStatus]
,(select title from StatusList where idStatus=[NewStatus]) as [NewStatus]
FROM [statusLog] , Users where
statusLog.IdUser=Users.IdUser
编辑2:
答案 0 :(得分:1)
从这个开始(你必须加入用户和你想要的东西)
SELECT
SL.DateOfChange AS StatusFrom
, SLN.StatusTo AS
, DATEDIFF(HOUR, SL.DateOfChange, SLN.StatusTo) AS StatusDuration
FROM
StatusLog SL
OUTER APPLY (
SELECT TOP(1)
DateOfChange AS StatusTo
FROM
StatusLog SLT
WHERE
SL.IdUser = SLT.IdUser
AND SLT.DateOfChange > SL.DateOfChange
ORDER BY
SLT.DateOfChange ASC
) SLN
INNER JOIN Users U
ON SL.IdUser = U.IdUser
INNER JOIN StatusList SLO -- Old status
ON SL.OldStatus = SLO.idStatus
INNER JOIN StatusList SLC -- Current status
ON SL.NewStatus = SLC.idStatus
APPLY运算符允许您为查询的外表表达式返回的每一行调用表值函数。表值函数用作右输入,外表表达用作左输入。从左输入的每一行评估右输入,并将生成的行组合起来用于最终输出。 APPLY运算符生成的列的列表是左输入中的列集,后跟右输入返回的列列表。 From MSDN: Using APPLY
作为旁注:表值函数也可以是子查询。
我建议您使用显式连接语法(INNER JOIN
)而不是隐式连接语法(列出表并使用WHERE条件)。
答案 1 :(得分:1)
这是一个SQL查询,只需使用子选择返回所需的结果:
SELECT [users].MemberId,
[users].FirstName,
[users].LastName,
thisLog.DateOfChange,
statusList1.title as OldStatus,
statuslist2.title as NewStatus,
(SELECT TOP 1 DATEDIFF(hour,lastLog.DateOfChange,thisLog.DateOfChange)
from [dbo].[statusLog] lastLog WHERE lastLog.DateOfChange<thisLog.DateOfChange
ORDER BY DateOfChange desc ) AS HoursSinceLastChange
FROM [dbo].[statusLog] thisLog
INNER JOIN [users] ON [users].IdUser=thisLog.IdUSer
INNER JOIN StatusList statusList1 ON statusList1.idStatus=thisLog.OldStatus
INNER JOIN StatusList statusList2 on statusList2.idStatus=thisLog.Newstatus
order by DateOfChange desc
希望我的所有列和表名都正确无误。
答案 2 :(得分:0)
当我遇到这个问题时,我插入临时表。这可以让你做很多很酷的事情。
declare @tab TABLE([Date1] TIME, [DATE2] TIME)
Insert @tab(SELECT DATE1 ,DATE2 FROM WHEREVER)
SELECT (DATEDIFF(DATE1,DATE2) AS Difference FROM @tab