我有一个表格,格式如下:
+----+------------+----------+------------+------------+
| id | cur_date | cur_hour | hour_count | cumu_count |
+----+------------+----------+------------+------------+
| 56 | 2016-01-01 | 00:00:00 | 8 | 8 |
| 57 | 2016-01-01 | 01:00:00 | 6 | 14 |
| 58 | 2016-01-01 | 02:00:00 | 3 | 17 |
| 59 | 2016-01-01 | 03:00:00 | 1 | 18 |
| 60 | 2016-01-01 | 04:00:00 | 6 | 24 |
| 61 | 2016-01-01 | 05:00:00 | 7 | 31 |
| 62 | 2016-01-01 | 06:00:00 | 29 | 60 |
| 63 | 2016-01-01 | 07:00:00 | 35 | 95 |
| 64 | 2016-01-01 | 08:00:00 | 49 | 144 |
| 65 | 2016-01-01 | 09:00:00 | 63 | 207 |
| 66 | 2016-01-01 | 10:00:00 | 64 | 271 |
| 67 | 2016-01-01 | 11:00:00 | 62 | 333 |
| 68 | 2016-01-01 | 12:00:00 | 57 | 390 |
| 69 | 2016-01-01 | 13:00:00 | 58 | 448 |
| 70 | 2016-01-01 | 14:00:00 | 48 | 496 |
| 71 | 2016-01-01 | 15:00:00 | 36 | 532 |
| 72 | 2016-01-01 | 16:00:00 | 22 | 554 |
| 73 | 2016-01-01 | 17:00:00 | 18 | 572 |
| 74 | 2016-01-01 | 18:00:00 | 16 | 588 |
| 75 | 2016-01-01 | 19:00:00 | 10 | 598 |
| 76 | 2016-01-01 | 20:00:00 | 7 | 605 |
| 77 | 2016-01-01 | 21:00:00 | 4 | 609 |
| 78 | 2016-01-01 | 22:00:00 | 2 | 611 |
| 79 | 2016-01-01 | 23:00:00 | 1 | 612 |
+----+------------+----------+------------+------------+
我想要做的是每天(我提供一个月),并返回具有最多字段的day,hour和hour_count。在这种情况下,10:00:00具有最高计数的小时,因此它应该返回
+-----------|------------|------------+
|cur_date | cur_hour | hour_count|
+-----------+------------+------------+
|2016-01-01 | 10:00:00 | 65 |
|2016-01-02 | ? | ? |
我正在使用以下查询,但它没有返回准确的结果:
mysql> select cur_date,cur_hour, max(hour_count) from hour_data where cur_date like '2016%' group by cur_date;
+------------+----------+-----------------+
| cur_date | cur_hour | max(hour_count) |
+------------+----------+-----------------+
| 2016-01-01 | 00:00:00 | 64 |
| 2016-01-02 | 04:00:00 | 67 |
| 2016-01-03 | 04:00:00 | 43 |
| 2016-01-04 | 05:00:00 | 640 |
| 2016-01-05 | 00:00:00 | 672 |
| 2016-01-06 | 00:00:00 | 675 |
| 2016-01-07 | 00:00:00 | 705 |
| 2016-01-08 | 00:00:00 | 696 |
| 2016-01-09 | 00:00:00 | 203 |
| 2016-01-10 | 00:00:00 | 89 |
| 2016-01-11 | 01:00:00 | 673 |
| 2016-01-12 | 00:00:00 | 708 |
| 2016-01-13 | 00:00:00 | 710 |
| 2016-01-14 | 00:00:00 | 731 |
| 2016-01-15 | 00:00:00 | 675 |
| 2016-01-16 | 00:00:00 | 222 |
| 2016-01-17 | 00:00:00 | 112 |
| 2016-01-18 | 00:00:00 | 683 |
| 2016-01-19 | 00:00:00 | 709 |
| 2016-01-20 | 00:00:00 | 690 |
| 2016-01-21 | 00:00:00 | 696 |
| 2016-01-22 | 00:00:00 | 643 |
| 2016-01-23 | 00:00:00 | 282 |
| 2016-01-24 | 00:00:00 | 189 |
| 2016-01-25 | 01:00:00 | 757 |
| 2016-01-26 | 00:00:00 | 765 |
| 2016-01-27 | 00:00:00 | 770 |
| 2016-01-28 | 00:00:00 | 805 |
| 2016-01-29 | 00:00:00 | 786 |
| 2016-01-30 | 00:00:00 | 394 |
| 2016-01-31 | 00:00:00 | 146 |
| 2016-02-01 | 00:00:00 | 797 |
+------------+----------+-----------------+
32 rows in set, 1 warning (0.01 sec)
日期和最大值(hour_count)是正确的,但cur_hour不是正确的小时。
答案 0 :(得分:0)
我决定采用脚本方法来解决问题。