假设我的数据如下:
Item Count
======== ========
1 123
2 1
3 47
4 117
5 18
6 466
7 202
我想创建一个给我这个的查询:
Count Start Count End Occurrences
=========== =========== ===========
0 100 3
101 200 2
201 300 1
301 400 0
401 500 1
基本上,我想采取一系列计数并将它们分组到统计汇总的范围内。我认为我没有使用正确的关键字来找到答案。我反对Oracle,但如果有一个ANSI SQL的答案,我很乐意拥有它。
答案 0 :(得分:1)
select
a.mini,
a.maxi,
count(a.item)
from
(
select
table.item,
case (table.counter)
when counter>=0 and counter<=100 then 0
when counter>100 and counter<200 then 101
when ....
end as mini
table.item,
case (table.counter)
when counter>=0 and counter<=100 then 100
when counter>100 and counter<200 then 201
when ....
end as maxi
from
table
) a
group by
a.mini,
a.maxi
答案 1 :(得分:0)
一种方法是使用CASE语句。如果您希望它可扩展,请尝试将范围放在单独的表中,并使用JOIN计算出现的次数。