2

サーバーリクエストのテーブルがあります-それぞれにタイムスタンプがあります。私が達成したいのは、クエリ (またはできればストアド プロシージャ) の結果が、1 日について、1 日の各時間 (0 から 23 まで) とその日の各分を含むテーブルを提供することです。時間 (0 から 59 まで)。交差するデータは、1 分あたり、1 時間あたりのリクエスト数を表示します。

私がこれまでに試したことは、1 日の時間ごとに分をグループ化し、それを Excel で操作して、次を使用して 24x60 マトリックスを生成することです。

SELECT DatePart(HH,Timestamp) as [Hour]
      , DatePart(MI,Timestamp) as [Minute]
    , Count(*) as [Count]

FROM Requests 
WHERE DatePart(Day,Timestamp) = 12

GROUP BY DatePart(HH,Timestamp), 
    DatePart(MI,Timestamp)

ORDER BY DatePart(HH,Timestamp)
    , DatePart(MI,Timestamp)

お時間をいただきありがとうございます:)

4

1 に答える 1

1

データベースがピボットをサポートしている場合、これを短くすることができます。その分にデータがない場合、分が失われます。

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;

フィドルの例

于 2013-09-14T12:31:49.590 に答える