Oracle sql 聚合查询结果

问题描述 投票:0回答:1

我有这张桌子:

我想要一个查询,根据我员工的职业和年龄为我提供这些结果:

sql mysql sqlite oracle-sqldeveloper oracle-apex
1个回答
0
投票

您设置的年龄范围有误。一个 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>
© www.soinside.com 2019 - 2024. All rights reserved.