Oracle:如何“分组"超过一个范围?
如果我有一张这样的桌子:
If I have a table like this:
pkey age
---- ---
1 8
2 5
3 12
4 12
5 22
我可以分组"以获取每个年龄的计数.
I can "group by" to get a count of each age.
select age,count(*) n from tbl group by age;
age n
--- -
5 1
8 1
12 2
22 1
我可以使用什么查询来按年龄范围分组?
What query can I use to group by age ranges?
age n
----- -
1-10 2
11-20 2
20+ 1
我在使用 10gR2,但我也对任何特定于 11g 的方法感兴趣.
I'm on 10gR2, but I'd be interested in any 11g-specific approaches as well.
推荐答案
SELECT CASE
WHEN age <= 10 THEN '1-10'
WHEN age <= 20 THEN '11-20'
ELSE '21+'
END AS age,
COUNT(*) AS n
FROM age
GROUP BY CASE
WHEN age <= 10 THEN '1-10'
WHEN age <= 20 THEN '11-20'
ELSE '21+'
END
相关文章