按 X 天范围分组

发布于 2024-10-12 22:22:05 字数 146 浏览 3 评论 0原文

我有一组或多条记录,我想对它们进行计数并按一定范围进行分组,例如我想对 X 天组创建的记录进行计数

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

I have a set or records and I want to count and group them by a certain range e.g. I want to count the records that were created by groups of X days

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

网白 2024-10-19 22:22:05

这是一个带有日期的示例。

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)

Here is an example with dates.

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)
南城追梦 2024-10-19 22:22:05

你可以做类似的事情
SELECT COUNT(*) FROM table GROUP BY FLOOR(created / 3)

...我想。

尽管如果 created 是日期字段,则您必须进行更多调整才能将其转换为数字值才能正常工作。

You can do something like
SELECT COUNT(*) FROM table GROUP BY FLOOR(created / 3)

... I think.

Although if created is a date field, you'll have to do a little more jiggering to get it into a number value for this to work.

浅忆 2024-10-19 22:22:05

谢谢@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);

Just get 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 |
+-------------+--------------+--------------+----------+

这可能更合适。

Thanks @Ronnis, I use your example and finally solve my problem.

And there is a small mistake I found, in example, I add one row

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

Now I get:

+----------+--------------+--------------+----------+
| 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 |
+----------+--------------+--------------+----------+

As you can see that days divide into 2, 3, 3, 3, 2 which not what I expected.

I change SQL as

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);

Just get datediff from created to start date, and now I get:

+-------------+--------------+--------------+----------+
| 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 |
+-------------+--------------+--------------+----------+

That might be more suitable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文