mysql 按X天的范围分组

vyu0f0g1  于 2023-04-28  发布在  Mysql
关注(0)|答案(3)|浏览(211)

我有一组记录,我想按一定的范围e来计数和分组。g.我想统计X天组创建的记录

e.g. SELECT COUNT(*) FROM `table` GROUP BY /*`created` 3 days/*
4dbbbstv

4dbbbstv1#

这里有一个日期的例子。

create table t1(created date not null);

insert 
  into t1(created) values (date '2011-01-09')
                         ,(date '2011-01-10')
                         ,(date '2011-01-11')
                         ,(date '2011-01-12')
                         ,(date '2011-01-13')
                         ,(date '2011-01-14')
                         ,(date '2011-01-15')
                         ,(date '2011-01-16')
                         ,(date '2011-01-17')
                         ,(date '2011-01-18')
                         ,(date '2011-01-19')
                         ,(date '2011-01-20');

select floor(datediff(now(), created) / 3) * 3 as days_ago
      ,min(created)
      ,max(created)
      ,count(*)
  from t1
 group 
    by floor(datediff(now(), created) / 3);

+----------+--------------+--------------+----------+
| days_ago | min(created) | max(created) | count(*) |
+----------+--------------+--------------+----------+
|        0 | 2011-01-18   | 2011-01-20   |        3 |
|        3 | 2011-01-15   | 2011-01-17   |        3 |
|        6 | 2011-01-12   | 2011-01-14   |        3 |
|        9 | 2011-01-09   | 2011-01-11   |        3 |
+----------+--------------+--------------+----------+
4 rows in set (0.00 sec)
xzv2uavs

xzv2uavs2#

你可以这样做
SELECT COUNT(*) FROM table GROUP BY FLOOR(created / 3)
我想是的
虽然如果created是一个日期字段,你必须做更多的调整,使它成为一个数字值,这样才能工作。

wlzqhblo

wlzqhblo3#

谢谢@Ronnis,我用你的例子,终于解决了我的问题。
还有一个小错误我发现,在例子中,我添加了一行

insert into t1(created) values (date '2011-01-21')

现在我得到:

+----------+--------------+--------------+----------+
| days_ago | min(created) | max(created) | count(*) |
+----------+--------------+--------------+----------+
|     1986 | 2011-01-20   | 2011-01-21   |        2 |
|     1989 | 2011-01-17   | 2011-01-19   |        3 |
|     1992 | 2011-01-14   | 2011-01-16   |        3 |
|     1995 | 2011-01-11   | 2011-01-13   |        3 |
|     1998 | 2011-01-09   | 2011-01-10   |        2 |
+----------+--------------+--------------+----------+

正如你所看到的,日子分成了2,3,3,3,2,这不是我所期望的。
我将SQL更改为

select floor(datediff(created, '2011-01-09') / 3) * 3 as days_before
      ,min(created)
      ,max(created)
      ,count(*)
from t1
group by floor(datediff(created, '2011-01-09') / 3);

只需获取从created到start date的datediff,现在我得到:

+-------------+--------------+--------------+----------+
| days_before | min(created) | max(created) | count(*) |
+-------------+--------------+--------------+----------+
|           0 | 2011-01-09   | 2011-01-11   |        3 |
|           3 | 2011-01-12   | 2011-01-14   |        3 |
|           6 | 2011-01-15   | 2011-01-17   |        3 |
|           9 | 2011-01-18   | 2011-01-20   |        3 |
|          12 | 2011-01-21   | 2011-01-21   |        1 |
+-------------+--------------+--------------+----------+

那可能更合适。

相关问题