简体   繁体   English

Mysql:按一天中的不同时段进行分组

[英]Mysql: grouping by day and hours of day

I have a database, and I've been trying for a while, but I'm not getting the results I want. 我有一个数据库,并且已经尝试了一段时间,但是没有得到想要的结果。 Here's a sample of what I have: 这是我所拥有的样本:

+---------+---------------------+
| Ammount | Date                |
+---------+---------------------+
|       1 | 2015-08-25 14:07:00 |
|       1 | 2015-08-25 14:12:00 |
|       1 | 2015-08-25 15:17:00 |
|       2 | 2015-08-25 15:22:00 |
|       1 | 2015-08-25 14:27:00 |
|       6 | 2015-08-25 14:32:00 |
|       1 | 2015-08-26 14:37:00 |
|       5 | 2015-08-26 14:42:00 |
|       1 | 2015-08-26 16:47:00 |
|       2 | 2015-08-26 16:52:00 |
+---------+---------------------+

And this is my query: 这是我的查询:

select Ammount, Date from table;

What I want to do is group by the day AND the hours of each day, and sum it, pretty much like this: 我想要做的是按天和每天的时间进行分组,并将其相加,非常像这样:

select sum(Ammount), Date from table group by hour(day(Date));

Except it groups everything together. 除了它将所有内容组合在一起。

SELECT SUM(Ammount), Date
FROM TABLE
GROUP BY YEAR(Date),
         MONTH(Date),
         DAY(Date),
         HOUR(Date);
select sum(Ammount), Date from table group by month(Date), day(Date), hour(Date);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM