单个选择或存储过程中的热图

时间:2013-09-14 12:25:35

标签: sql stored-procedures

我有一个服务器请求表 - 每个都有一个时间戳。我想要完成的是,我的查询结果(或者最好是存储过程)给了我一天的表格,其中一天中的每个小时(从0到23)和每一分钟小时向下(从0到59)。交叉数据将显示每小时每小时的请求数。

到目前为止我尝试过的是按时间分组,并使用以下方法操作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)

非常感谢任何帮助,提前感谢您的时间:)

1 个答案:

答案 0 :(得分:1)

如果您的数据库支持pivot,则可以缩短此时间。如果那一分钟没有数据,它将错过分钟。

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)

使用Pivot的版本:

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;

Example Fiddle