SQL Server: Average counts by hour and day of week

前端 未结 2 927
Happy的楠姐
Happy的楠姐 2021-02-09 00:13

Background

I have a table set up in a SQL Server environment that contains a log of various activity that I\'m tracking. Particular log items use unique codes to categ

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-09 00:38

    ; WITH a AS (
        SELECT CAST([time] AS date) AS ForDate
           , DATEPART(hour, [time]) AS OnHour
           , txtW=DATENAME(WEEKDAY,[time])
           , intW=DATEPART(WEEKDAY,[time])
           , Totals=COUNT(*)
        FROM [log] WHERE [code] = 'tib_imp.8'
        GROUP BY CAST(time AS date)
        , DATENAME(WEEKDAY,[time])
        , DATEPART(WEEKDAY,[time])
        , DATEPART(hour,[time])
    )
    SELECT [Day]=txtW
    , [Hour]=OnHour
    , [Average Count]=AVG(Totals)
    FROM a
    GROUP BY txtW, intW, OnHour
    ORDER BY intW, OnHour
    

提交回复
热议问题