我有 SQL 表,例如 DateDiff 的格式(hh.mm)
DateDiff ATM
1.45 121
1.50 121
1.50 121
当我在 ATM 上执行Sum
DateDiff 时,Group by
它会显示如下结果
4.45 121
但实际的日期时差应该是
5.25 121
我们如何在 SQL Query 中实现相同的功能
Select Sum(Cast(Convert(Varchar(10),DATEDIFF(HOUR,TicketRaisedOn,ClosedOn))+'.'+
Convert(Varchar(10),DATEDIFF(MINUTE,TicketRaisedOn,ClosedOn)%60) as Numeric(18,2)))[Down Time],ATM
From Ticket Where Closed=1 And DATEPART(DAYOFYEAR,GETDATE())=DATEPART(DAYOFYEAR,TicketRaisedOn)
Group BY ATM Order By [Down Time] Desc
TicketRaisedOn
&ClosedOn
属于日期时间
数据库是 SQL Server 2008
上面的查询将打印这样的结果(但它是错误的,因为它将作为数字而不是日期时间格式求和)
Down Time ATM
16.95 282
14.46 1811
14.20 52
14.04 936
样本数据
Select TicketRaisedOn,ClosedOn,ATM
From Ticket
Where ATM=282 And DATEPART(DAYOFYEAR,GETDATE())=DATEPART(DAYOFYEAR,TicketRaisedOn)
And Closed=1
TicketRaisedOn ClosedOn ATM
2012-12-21 01:15:23.793 2012-12-21 15:11:59.240 282
2012-12-21 16:42:29.820 2012-12-21 18:21:30.797 282