我有下表。我试图找到标记为1的员工数与按部门划分的员工总数的比率。任何的想法?我已经尝试将这两个表连在一起,只是将标记为一个的员工数量增加一倍
这是我的专栏:
flag department employees
-----------------------------
0 892684 10
0 893432 29
0 893561 4
0 896114 2
0 897783 18
1 897783 77
0 897853 7
我想要得到结果:
department ratio
-----------------
897783 .81
892684 0
等
答案 0 :(得分:1)
这是标准SQL,应该在所有DBMS中按原样运行:
select
department,
sum(case when flag = 1 then employees else 0 end) -- flagged employees only
/ cast(sum(employees) as float) -- all employees
as ratio
from tab
group by department;