按范围分组24小时

时间:2015-01-24 20:01:13

标签: mysql aggregate-functions

我有很多行,像这样的24小​​时格式

enter image description here

我想要的是像这样的格式

+------+------+----------+
| from | to   | count(*) |
+------+------+----------+
| 0000 |  0100|        3 |
| 0100 | 0200 |        4 |
|  0200| 0300 |        2 |
| 0300 | 0400 |        1 |
+------+------+----------+

我试过这个

SELECT COUNT(*) `count`,
       100*FLOOR(the_amount/100) `from`,
       100*FLOOR(the_amount/100)+99 `to`
  FROM r_data
 where transaction_type = 'send'
 GROUP BY FLOOR(the_amount/100) ORDER BY COUNT(*) DESC

但是这只给了一行。我该如何写一个查询来分组24小时格式的时间?。

1 个答案:

答案 0 :(得分:2)

试试这个:

SELECT
  LPAD(100*FLOOR(24_hour_time/100),4,0) AS `from`,
  LPAD(100*FLOOR(24_hour_time/100) + 100,4,0) AS `to`,
  COUNT(*) AS `count`
FROM
  r_data
WHERE
  transaction_typ = 'send'
GROUP BY 1
ORDER BY 24_hour_time;

但它只会显示“人口充足的时间”。