SQL Query to return 24 hour, hourly count even when no values exist?

前端 未结 6 1455
-上瘾入骨i
-上瘾入骨i 2021-01-14 08:53

I\'ve written a query that groups the number of rows per hour, based on a given date range.

SELECT CONVERT(VARCHAR(8),TransactionTime,101) + \' \' + CONVERT(         


        
6条回答
  •  無奈伤痛
    2021-01-14 09:23

    You do this by building first the 23 hours table, the doing an outer join against the transactions table. I use, for same purposes, a table valued function:

    create function tvfGetDay24Hours(@date datetime)
    returns table
    as return (
    select dateadd(hour, number, cast(floor(cast(@date as float)) as datetime)) as StartHour
      , dateadd(hour, number+1, cast(floor(cast(@date as float)) as datetime)) as EndHour
    from master.dbo.spt_values
    where number < 24 and type = 'p');
    

    Then I can use the TVF in queries that need to get 'per-hour' basis data, even for missing intervals in the data:

    select h.StartHour, t.TotalHourlyTransactions
    from tvfGetDay24Hours(@StartDate) as h
    outer apply (
      SELECT 
        COUNT(TransactionID) AS TotalHourlyTransactions
        FROM MyTransactions 
        WHERE TransactionTime BETWEEN h.StartHour and h.EndHour
        AND TerminalId = @TerminalID) as t
    order by h.StartHour
    

    Updated

    Example of a TVF that returns 24hours between any arbitrary dates:

    create function tvfGetAnyDayHours(@dateFrom datetime, @dateTo datetime)
    returns table
    as return (
    select dateadd(hour, number, cast(floor(cast(@dateFrom as float)) as datetime)) as StartHour
      , dateadd(hour, number+1, cast(floor(cast(@dateFrom as float)) as datetime)) as EndHour
    from master.dbo.spt_values
    where type = 'p'
    and number < datediff(hour,@dateFrom, @dateTo) + 24);
    

    Note that since master.dbo.spt_values contains only 2048 numbers, the function will not work between dates further apart than 2048 hours.

提交回复
热议问题