我希望将计数分为(0 +,5 +,10 +,15 +等)类别
因此,具有7个潜在客户的代理商应计入0 +,5 +组,而不是10 +,15 +。
Postgres查询:
WITH agent_stats AS (
SELECT agent_id, FLOOR(COUNT(*)/5) AS count_category
FROM leads
GROUP BY 1
)
SELECT count_category, COUNT(*)
FROM agent_stats
GROUP BY 1
ORDER BY 1
结果:
| count_category | count |
| 0 | 12 |
| 5 | 18 |
| 15 | 9 |
| 20 | 4 |
所需:
| count_category | count |
| 0 | 43 |
| 5 | 31 |
| 15 | 13 |
| 20 | 4 |
答案 0 :(得分:0)
最简单的方法可能是累积总和:
WITH agent_stats AS (
SELECT agent_id, FLOOR(COUNT(*)/5) AS count_category
FROM leads
GROUP BY 1
)
SELECT count_category, COUNT(*),
SUM(COUNT(*)) OVER (ORDER BY count_category DESC)
FROM agent_stats
GROUP BY 1
ORDER BY 1;