我喜歡在哪裏DateDiff的是格式(hh.mm)總和日期時間差(HH.MM)
DateDiff ATM
1.45 121
1.50 121
1.50 121
,當我在ATM做則DateDiff的Sum
通過Group by
它會顯示結果的SQL表像
4.45 121
但實際的日期時間差異應
5.25 121
我們如何能chieve相同的SQL查詢
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
我懷疑爲數字(18,2)談到了轉換後的前總和 –
數據是否高於查詢結果?如果是這樣,那麼你可以發佈表中的數據嗎? – Taryn
一些數據和表格結構域'TicketRaisedOn,ClosedOn,Closed'? – bonCodigo