Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 基于范围聚合列值_Sql_Postgresql_Count_Aggregate - Fatal编程技术网

Sql 基于范围聚合列值

Sql 基于范围聚合列值,sql,postgresql,count,aggregate,Sql,Postgresql,Count,Aggregate,我不熟悉SQL和stackoverflow,如果我的问题很琐碎,请原谅。我在一个表中记录了客户的购买数量,所以我想统计一下购买量在一定范围内的客户数量 TABLE: +-------------+----------------+ | customer_id | order_quantity | +-------------+----------------+ | 123 | 10000 | | 143 | 5000 | |

我不熟悉SQL和stackoverflow,如果我的问题很琐碎,请原谅。我在一个表中记录了客户的购买数量,所以我想统计一下购买量在一定范围内的客户数量

TABLE:
+-------------+----------------+
| customer_id | order_quantity |
+-------------+----------------+
|         123 |          10000 |
|         143 |           5000 |
|         999 |         200000 |
|         555 |          50000 |
+-------------+----------------+
目标是统计购买量小于5000的客户数量,介于5000-50000和50000-100000之间

我用过:

SELECT customer_id,
      CASE
         WHEN COUNT(order_quantity) < 5000
         ....
FROM purchases
这不正确,甚至不起作用。

您可以使用:

select (case when order_quantity < 5000 then '[0-5000)'
             when order_quantity < 10000 then '[5000-10000)'
             else '10000+'
        end) as grp,
       count(*) as num_purchases,
       count(distinct customer_id) as num_customers
from t
group by grp
order by min(order_quantity);

如果客户在给定的组中进行了多个购买,则不清楚您是要计算购买数量还是要计算客户数量。这两者都可以。

不起作用不是一个有意义的描述。您想要什么输出?此外,如果要计数,SQL语句中是否有计数?