我正在尋找使用datepart運行SQL查詢將數據分成小時增量,然後使該查詢運行多天。目前,我只是將日期遞增一天,然後運行多個查詢。我想知道是否有一種方法可以將這一切合併爲一個查詢,以便爲整個日期範圍提供輸出。SQL Server - 按小時查詢多天
select datepart(hour, datetime), sum(calls)
from dbo.gateway_concurrent_calls
where datetime between 'MM/DD/YY' and 'MM/DD+1/YY'
group by datepart(hour, datetime)
這給出了這個輸出。
0 1220
1 569
2 391
3 313
4 161
5 68
6 42
7 24
8 19
9 18
10 23
11 45
12 90
13 311
14 566
15 668
16 688
17 735
18 718
19 729
20 701
21 699
22 683
23 570
除非有什麼我不知道的,轉換應該看起來像'convert(date,datetime)'或像'cast(datetime as date)'。除此之外,它是+1。 –