您设置的年龄范围有误。一个 60 岁的人与任何地方都格格不入。这就是为什么我有点修复它。
如果您认为我做错了,请随时纠正。
样本数据:
SQL> with test (employee, age, occupation) as
2 (select 'Bob' , 52, 'Doctor' from dual union all
3 select 'Greg' , 42, 'Doctor' from dual union all
4 select 'Steve', 48, 'IT' from dual union all
5 select 'Paul' , 38, 'IT' from dual union all
6 select 'Bill' , 29, 'IT' from dual union all
7 select 'Nick' , 39, 'Manager' from dual
8 )
查询:
9 select occupation,
10 sum(case when age between 21 and 30 then 1 else 0 end) "21-30",
11 sum(case when age between 31 and 50 then 1 else 0 end) "31-50",
12 sum(case when age between 51 and 60 then 1 else 0 end) "51-60",
13 sum(case when age between 61 and 70 then 1 else 0 end) "61-70"
14 from test
15 group by occupation;
OCCUPATION 21-30 31-50 51-60 61-70
--------------- ---------- ---------- ---------- ----------
Doctor 0 1 1 0
IT 1 2 0 0
Manager 0 1 0 0
SQL>