我有2行,如下例所示。
我想在一定的消费间隔中选择我有多少客户。
例:1000-3000,3000-5000 ......
这是我的SQL查询:
SELECT t.consumption, COUNT(DISTINCT t.id) as numberOfCustomers <br>
FROM(
SELECT customer.id, SUM(order.value) AS consumption
FROM order <br>
INNER JOIN customer ON order.customerId = customer.id
GROUP BY customer.id) AS t
GROUP BY t.consumption
答案 0 :(得分:0)
您可以使用此功能创建范围:
CONCAT((FLOOR(t.consumption/2000)) * 2000, '-',
((FLOOR(t.consumption/2000)) * 2000) + 1999) -- the step is 2000
然后按此范围制作总和组