将SQL结果分组/聚合为1小时存储桶

时间:2022-06-20 11:44:59

Similar to this question, I need to group a large number of records into 1-hour "buckets". For example, let's say I've got a typical ORDER table with a datetime attached to each order. And I want to see the total number of orders per hour. So I'm using SQL roughly like this:

与此问题类似,我需要将大量记录分组为1小时的“桶”。例如,假设我有一个典型的ORDER表,每个订单附有一个日期时间。我希望看到每小时的订单总数。所以我大致使用SQL:

SELECT datepart(hh, order_date), SUM(order_id)
FROM ORDERS
GROUP BY datepart(hh, order_date)

The problem is that if there are no orders in a given 1-hour "bucket", no row is emitted into the result set. I'd like the resultset to have a row for each of the 24 hour, but if no orders were made during a particular hour, just record the number of orders as O.

问题是如果给定的1小时“桶”中没有订单,则不会向结果集中发出任何行。我希望结果集在24小时内每行都有一行,但如果在特定小时内没有订单,只需将订单数记录为O.

Is there any way to do this in a single query?

有没有办法在单个查询中执行此操作?

See also Getting Hourly Statistics Using SQL.

另请参阅使用SQL获取每小时统计信息。

3 个解决方案

#1


7  

You need to have a pre-populated table (or a function returning a table result set) to join with, that contains all the 1-hour slots you want in your result.

您需要预先填充的表(或返回表结果集的函数)才能加入,其中包含结果中所需的所有1小时槽。

Then you do a OUTER JOIN with that, and you should get them all.

然后你用它进行OUTER JOIN,你应该得到它们。

Something like this:

像这样的东西:

SELECT SLOT_HOUR, SUM(order_id)
FROM
    ONEHOURSLOTS
    LEFT JOIN ORDERS ON DATEPART(hh, order_date) = SLOT_HOUR
GROUP BY SLOT_HOUR

#2


8  

Some of the previous answers recommend using a table of hours and populating it using a UNION query; this can be better done with a Common Table Expression:

以前的一些答案建议使用小时表并使用UNION查询填充它;使用公用表表达式可以做得更好:

; WITH [Hours] ([Hour]) AS
(
SELECT TOP 24 ROW_NUMBER() OVER (ORDER BY [object_id]) AS [Hour]
FROM sys.objects
ORDER BY [object_id]
)
SELECT h.[Hour], o.[Sum]
FROM [Hours] h
LEFT OUTER JOIN (
   SELECT datepart(hh, order_date) as [Hour], SUM(order_id) as [Sum]
      FROM Orders
      GROUP BY datepart(hh, order_date) 
) o
ON h.[Hour] = o.[Hour]

#3


2  

Create a table of hours, either persisted or even synthesized 'on the fly':

创建一个小时表,可以持久保存,甚至可以“动态”合成:

SELECT h.hour, s.sum
FROM (
   SELECT 1 as hour
   UNION ALL SELECT 2
   UNION ALL SELECT 3
   ...
   UNION ALL SELECT 24) as h
LEFT OUTER JOIN  (
   SELECT datepart(hh, order_date) as hour, SUM(order_id) as sum
      FROM ORDERS
      GROUP BY datepart(hh, order_date) ) as s 
  ON h.hour = s.hour;

#1


7  

You need to have a pre-populated table (or a function returning a table result set) to join with, that contains all the 1-hour slots you want in your result.

您需要预先填充的表(或返回表结果集的函数)才能加入,其中包含结果中所需的所有1小时槽。

Then you do a OUTER JOIN with that, and you should get them all.

然后你用它进行OUTER JOIN,你应该得到它们。

Something like this:

像这样的东西:

SELECT SLOT_HOUR, SUM(order_id)
FROM
    ONEHOURSLOTS
    LEFT JOIN ORDERS ON DATEPART(hh, order_date) = SLOT_HOUR
GROUP BY SLOT_HOUR

#2


8  

Some of the previous answers recommend using a table of hours and populating it using a UNION query; this can be better done with a Common Table Expression:

以前的一些答案建议使用小时表并使用UNION查询填充它;使用公用表表达式可以做得更好:

; WITH [Hours] ([Hour]) AS
(
SELECT TOP 24 ROW_NUMBER() OVER (ORDER BY [object_id]) AS [Hour]
FROM sys.objects
ORDER BY [object_id]
)
SELECT h.[Hour], o.[Sum]
FROM [Hours] h
LEFT OUTER JOIN (
   SELECT datepart(hh, order_date) as [Hour], SUM(order_id) as [Sum]
      FROM Orders
      GROUP BY datepart(hh, order_date) 
) o
ON h.[Hour] = o.[Hour]

#3


2  

Create a table of hours, either persisted or even synthesized 'on the fly':

创建一个小时表,可以持久保存,甚至可以“动态”合成:

SELECT h.hour, s.sum
FROM (
   SELECT 1 as hour
   UNION ALL SELECT 2
   UNION ALL SELECT 3
   ...
   UNION ALL SELECT 24) as h
LEFT OUTER JOIN  (
   SELECT datepart(hh, order_date) as hour, SUM(order_id) as sum
      FROM ORDERS
      GROUP BY datepart(hh, order_date) ) as s 
  ON h.hour = s.hour;