データベースがピボットをサポートしている場合、これを短くすることができます。その分にデータがない場合、分が失われます。
Select
DatePart(MI,Timestamp) as Mi,
Sum(Case When DatePart(HH,Timestamp) = 0 Then 1 Else 0 End) as H0,
Sum(Case When DatePart(HH,Timestamp) = 1 Then 1 Else 0 End) as H1,
Sum(Case When DatePart(HH,Timestamp) = 2 Then 1 Else 0 End) as H2,
Sum(Case When DatePart(HH,Timestamp) = 3 Then 1 Else 0 End) as H3,
Sum(Case When DatePart(HH,Timestamp) = 4 Then 1 Else 0 End) as H4,
Sum(Case When DatePart(HH,Timestamp) = 5 Then 1 Else 0 End) as H5,
Sum(Case When DatePart(HH,Timestamp) = 6 Then 1 Else 0 End) as H6,
Sum(Case When DatePart(HH,Timestamp) = 7 Then 1 Else 0 End) as H7,
Sum(Case When DatePart(HH,Timestamp) = 8 Then 1 Else 0 End) as H8,
Sum(Case When DatePart(HH,Timestamp) = 9 Then 1 Else 0 End) as H9,
Sum(Case When DatePart(HH,Timestamp) = 10 Then 1 Else 0 End) as H10,
Sum(Case When DatePart(HH,Timestamp) = 11 Then 1 Else 0 End) as H11,
Sum(Case When DatePart(HH,Timestamp) = 12 Then 1 Else 0 End) as H12,
Sum(Case When DatePart(HH,Timestamp) = 13 Then 1 Else 0 End) as H13,
Sum(Case When DatePart(HH,Timestamp) = 14 Then 1 Else 0 End) as H14,
Sum(Case When DatePart(HH,Timestamp) = 15 Then 1 Else 0 End) as H15,
Sum(Case When DatePart(HH,Timestamp) = 16 Then 1 Else 0 End) as H16,
Sum(Case When DatePart(HH,Timestamp) = 17 Then 1 Else 0 End) as H17,
Sum(Case When DatePart(HH,Timestamp) = 18 Then 1 Else 0 End) as H18,
Sum(Case When DatePart(HH,Timestamp) = 19 Then 1 Else 0 End) as H19,
Sum(Case When DatePart(HH,Timestamp) = 20 Then 1 Else 0 End) as H20,
Sum(Case When DatePart(HH,Timestamp) = 21 Then 1 Else 0 End) as H21,
Sum(Case When DatePart(HH,Timestamp) = 22 Then 1 Else 0 End) as H22,
Sum(Case When DatePart(HH,Timestamp) = 23 Then 1 Else 0 End) as H23
From
Requests
Group By
DatePart(MI,Timestamp)
Order By
DatePart(MI,Timestamp)
ピボットを使用したバージョン:
Select
Mi,
[0], [1], [2], [3], [4], [5], [6], [7],
[8], [9], [10], [11], [12], [13], [14], [15],
[16], [17], [18], [19], [20], [21], [22], [23]
From (
select
datepart(mi, timestamp) mi,
datepart(hh, timestamp) hh
from
Requests
) as s
Pivot (
count(hh)
For
hh in (
[0], [1], [2], [3], [4], [5], [6], [7],
[8], [9], [10], [11], [12], [13], [14], [15],
[16], [17], [18], [19], [20], [21], [22], [23]
)
) as p
Order By mi;
フィドルの例