当前位置: 首页 > 面试题库 >

MySQL按小时分组

澹台阳秋
2023-03-14
问题内容

我正在尝试按小时使用量从历史记录表中获取报告。history表是;

CREATE TABLE IF NOT EXISTS `history` (
`history_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL DEFAULT '0',
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`history_id`),
KEY `user_id` (`user_id`),
KEY `created` (`created`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

我想通过组HOURCOUNT在指定日期范围内的所有记录。

Hours            |    Usage
------------------------------------
00:00-01:00      |      5
01:00-02:00      |      9
02:00-03:00      |      0 (or NULL)
03:00-04:00      |      20
...
...
...
22:00-23:00      |      11
23:00-00:00      |      1

我使用了类似这样的查询,但是它并不能显示全部时间。

SELECT

CASE 
  WHEN HOUR(created) BETWEEN 0 AND 1 THEN '00:00 - 01:00'
  WHEN HOUR(created) BETWEEN 1 AND 2 THEN '01:00 - 02:00'
  WHEN HOUR(created) BETWEEN 2 AND 3 THEN '02:00 - 03:00'
  WHEN HOUR(created) BETWEEN 3 AND 4 THEN '03:00 - 04:00'
  WHEN HOUR(created) BETWEEN 4 AND 5 THEN '04:00 - 05:00'
  WHEN HOUR(created) BETWEEN 5 AND 6 THEN '05:00 - 06:00'
  WHEN HOUR(created) BETWEEN 6 AND 7 THEN '06:00 - 07:00'
  WHEN HOUR(created) BETWEEN 7 AND 8 THEN '07:00 - 08:00'
  WHEN HOUR(created) BETWEEN 8 AND 9 THEN '08:00 - 09:00'
  WHEN HOUR(created) BETWEEN 9 AND 10 THEN '09:00 - 10:00'
  WHEN HOUR(created) BETWEEN 10 AND 11 THEN '10:00 - 11:00'
  WHEN HOUR(created) BETWEEN 11 AND 12 THEN '11:00 - 12:00'
  WHEN HOUR(created) BETWEEN 12 AND 13 THEN '12:00 - 13:00'
  WHEN HOUR(created) BETWEEN 13 AND 14 THEN '13:00 - 14:00'
  WHEN HOUR(created) BETWEEN 14 AND 15 THEN '14:00 - 15:00'
  WHEN HOUR(created) BETWEEN 15 AND 16 THEN '15:00 - 16:00'
  WHEN HOUR(created) BETWEEN 16 AND 17 THEN '16:00 - 17:00'
  WHEN HOUR(created) BETWEEN 17 AND 18 THEN '17:00 - 18:00'
  WHEN HOUR(created) BETWEEN 18 AND 19 THEN '18:00 - 19:00'
  WHEN HOUR(created) BETWEEN 19 AND 20 THEN '19:00 - 20:00'
  WHEN HOUR(created) BETWEEN 20 AND 21 THEN '20:00 - 21:00'
  WHEN HOUR(created) BETWEEN 21 AND 22 THEN '21:00 - 23:00'
  WHEN HOUR(created) BETWEEN 22 AND 23 THEN '22:00 - 23:00'
  WHEN HOUR(created) BETWEEN 23 AND 24 THEN '23:00 - 00:00'
END AS `Hours`,

COUNT(*) AS `usage`
FROM history
WHERE (created BETWEEN '2012-02-07' AND NOW())
GROUP BY 
  CASE 
    WHEN HOUR(created) BETWEEN 0 AND 1 THEN 1
    WHEN HOUR(created) BETWEEN 1 AND 2 THEN 2
    WHEN HOUR(created) BETWEEN 2 AND 3 THEN 3
    WHEN HOUR(created) BETWEEN 3 AND 4 THEN 4
    WHEN HOUR(created) BETWEEN 4 AND 5 THEN 5
    WHEN HOUR(created) BETWEEN 5 AND 6 THEN 6
    WHEN HOUR(created) BETWEEN 6 AND 7 THEN 7
    WHEN HOUR(created) BETWEEN 7 AND 8 THEN 8
    WHEN HOUR(created) BETWEEN 8 AND 9 THEN 9
    WHEN HOUR(created) BETWEEN 9 AND 10 THEN 10
    WHEN HOUR(created) BETWEEN 10 AND 11 THEN 11
    WHEN HOUR(created) BETWEEN 11 AND 12 THEN 12
    WHEN HOUR(created) BETWEEN 12 AND 13 THEN 13
    WHEN HOUR(created) BETWEEN 13 AND 14 THEN 14
    WHEN HOUR(created) BETWEEN 14 AND 15 THEN 15
    WHEN HOUR(created) BETWEEN 15 AND 16 THEN 16
    WHEN HOUR(created) BETWEEN 16 AND 17 THEN 17
    WHEN HOUR(created) BETWEEN 17 AND 18 THEN 18
    WHEN HOUR(created) BETWEEN 18 AND 19 THEN 19
    WHEN HOUR(created) BETWEEN 19 AND 20 THEN 20
    WHEN HOUR(created) BETWEEN 20 AND 21 THEN 21
    WHEN HOUR(created) BETWEEN 21 AND 22 THEN 22
    WHEN HOUR(created) BETWEEN 22 AND 23 THEN 23
    WHEN HOUR(created) BETWEEN 23 AND 24 THEN 24
END

仅在有记录时显示。

Hours            |    Usage
------------------------------------
00:00-01:00      |      5
01:00-02:00      |      9
23:00-00:00      |      1

问题答案:

您现有的查询可以简化为:

SELECT   CONCAT(HOUR(created), ':00-', HOUR(created)+1, ':00') AS Hours
  ,      COUNT(*) AS `usage`
FROM     history
WHERE    created BETWEEN '2012-02-07' AND NOW()
GROUP BY HOUR(created)

要显示 每一个 小时,包括那些没有数据,就需要外,其所有您想要的数据小时表连接。您可以使用UNION以下命令在查询中建立这样的表:

SELECT   CONCAT(Hour, ':00-', Hour+1, ':00') AS Hours
  ,      COUNT(created) AS `usage`
FROM     history
  RIGHT JOIN (
                   SELECT  0 AS Hour
         UNION ALL SELECT  1 UNION ALL SELECT  2 UNION ALL SELECT  3
         UNION ALL SELECT  4 UNION ALL SELECT  5 UNION ALL SELECT  6
         UNION ALL SELECT  7 UNION ALL SELECT  8 UNION ALL SELECT  9
         UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
         UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
         UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18
         UNION ALL SELECT 19 UNION ALL SELECT 20 UNION ALL SELECT 21
         UNION ALL SELECT 22 UNION ALL SELECT 23
  )      AS AllHours ON HOUR(created) = Hour
WHERE    created BETWEEN '2012-02-07' AND NOW() OR created IS NULL
GROUP BY Hour
ORDER BY Hour

但是,对于没有数据的组的处理实际上是业务逻辑的问题,最好将其放置在数据访问层而不是数据库本身中:实际上,对于您的应用程序,每隔一小时使用零值应该是微不足道的缺席。



 类似资料:
  • 问题内容: 我有以下查询: 上面的查询仅返回进行呼叫的时间: 期望的输出应该是所有小时,并且在没有呼叫的情况下,该小时应该是0个呼叫,如下所示: 问题答案: 首先,您的查询可以用一种更简单的方式表示为: 使用@NoDisplayName在他们的答案中提出的想法: 您可以加入包含小时数的表,以获取所需的结果: 如果运行太慢(我确定运行速度很慢),则应该研究一种使用类似方法的方法,而不是比较和。 可以

  • 问题内容: 我有一个包含会话事件时间戳的表。我想根据时间戳查询两个会话之间至少有10分钟的事件间隔时,我有多少会话。我可以使用MySql查询来计算会话数吗? 谢谢 问题答案: 在您的桌子上只有很少的信息,这对您来说只是一个基本的想法,但是您可以执行以下操作: 获取所有时间戳,并将它们与所有其他较大的时间戳合并,然后使用MIN将其缩小到下一个最大的时间戳。然后从中选择差异小于600秒的计数(假设使用

  • 问题内容: 我有一个数据集: 我想基本上在一个小时内“拆分”这些时间间隔,例如: 到目前为止,这是我的代码(该想法的积分发给@DumitrescuBogdan,将通话数据分为15分钟间隔): 后半部分(在“全部合并”之后)不产生任何结果,前半部分产生以下结果: 谢谢,我是初学者。我了解第一部分;坦白地说,我不明白下半场 我愿意接受其他解决方案。 我正在使用MS-Access 2010 问题答案:

  • 问题内容: 我有一个称为 activity_dt 的日期时间,数据如下所示: 如何按日期和小时分组? 问题答案: SQL Server: 甲骨文: MySQL的:

  • 问题内容: 有没有一种简单的方法可以将GROUP BY结果限制在前2位。以下查询返回所有结果。使用“ LIMIT 2”将整个列表减少到仅前2个条目。 结果如下: 谢谢乔恩 根据要求,我附上了表格结构和一些测试数据的副本。我的目标是创建一个视图,该视图具有每个唯一的rating_name的前2个结果 问题答案: 我认为MySQL中没有简单的方法。一种方法是通过为按rating_name分组的每一行生

  • 问题内容: 我有一个名为列是一个。因此,例如,平均外观值可能是:。我希望能够搜索并返回一天中特定时间的所有结果,而不考虑日期。 例如,如果我在列中搜索值,它将返回: 有什么想法吗? 问题答案: 您可以使用HOUR()函数: las,一旦您查询了几千行,该查询的性能将非常 糟糕 -函数不可索引,因此每次运行此查询时都会进行全表扫描。 在类似情况下的操作:创建了另一列,对其进行了索引,并在插入时填充了