如何选择按范围分组的值计数 [英] How to select the count of values grouped by ranges

查看:56
本文介绍了如何选择按范围分组的值计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

冰雹,堆叠!

我需要选择按范围分组的值计数.

I need to select the count of values grouped by ranges.

为了举例说明,假设我在表列中具有以下值:1,2,4,5,6,8,9,11,13,16

To exemplify, suppose that I have the following values in a table columm: 1,2,4,5,6,8,9,11,13,16

然后,我想在5的范围内减少它们的数量,如下所示:

Then, I want to retreave the count of them in ranges of 5, like this:

From  0 to  4 there is 3 values (1,2,4)
From  5 to  9 there is 4 values (5,6,8,9)
From 10 to 14 there is 2 values (11,13)
From 15 to 19 there is 1 values (16)

依此类推...

如何在查询中做到这一点?

How can I make this in a query?

推荐答案

也许这就是您想要的:

SELECT
    5 * (n div 5) as 'from',
    5 * (n div 5) + 4 as 'to',
    COUNT(*)
FROM yourtable
GROUP BY n div 5;

对于您的示例,此查询为您提供

For your sample this query gives you

+------+------+----------+
| from | to   | count(*) |
+------+------+----------+
|    0 |    4 |        3 |
|    5 |    9 |        4 |
|   10 |   14 |        2 |
|   15 |   19 |        1 |
+------+------+----------+
4 rows in set (0.00 sec)

这篇关于如何选择按范围分组的值计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆