在此示例中,如何使用SQL返回工资带的员工数量?
sip_cid_type
答案 0 :(得分:0)
试试这个:
select s.name, count(*) as count
from employeesalary e
inner join salarybands s on e.salary >= s.minsalary and e.salary <=s.maxsalary
group by s.name
关键的想法是根据每个员工的薪水范围来加入表格。然后按照通常的方式实施计算。
答案 1 :(得分:0)
非连接非常适合您的需求。它是一个不使用等价运算符来匹配行的连接。
SELECT
sb.name,
count(*)
FROM
EmployeeSalary es
INNER JOIN SalaryBands sb ON
es.salary BETWEEN sb.minsalary AND sb.maxsalary
GROUP BY sb.name
答案 2 :(得分:0)
另一种不需要连接的方法。
select
sum(case when REPLACE(salary, ',', '') between 20000 and 24999) then 1 else 0 end as count_tier1,
sum(case when REPLACE(salary, ',', '') between 25000 and 44999) then 1 else 0 end as count_tier2,
sum(case when REPLACE(salary, ',', '') between 45000 and 150000) then 1 else 0 end as count_tier3
from
Employee_salary
;