繁体   English   中英

在 SQL Server 中的单个查询中排列查询

[英]Arrange Queries in single query in SQL Server

我有一个包含员工、部门和薪水列的表。

  • 第 1 列:部门。
  • 第 2 列:工资在 (0 到 5000) 范围内的人数
  • 第 3 列:工资在 (5000 到 10000) 范围内的人数
  • 第 4 列:薪水在 (10000 到 15000) 范围内的人数
  • 第 5、6、7 列:工资总和,在第 2、3、4 列中使用范围。

多个薪资条件的单条查询如何排列?

使用条件聚合:

select
    department,
    sum(case when                    salary <= 5000  then 1 else 0 end) count_salary_500,
    sum(case when salary > 5000  and salary <= 10000 then 1 else 0 end) count_salary_501_1000,
    sum(case when salary > 10000 and salary <= 15000 then 1 else 0 end) count_salary_10001_15000,
    sum(case when salary                    <= 5000  then salary end) sum_salary_500,
    sum(case when salary > 5000  and salary <= 10000 then salary end) sum_salary_501_1000,
    sum(case when salary > 10000 and salary <= 15000 then salary end) sum_salary_10001_15000
from mytable
group by department

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM