我有一个员工表,有一列( empno,enme,salary,deptno )
我想编写一个显示下表的查询:包含第一列
,第二栏
名称开始的各部门员工的工资总额 与' A'
和第三栏
该部门所有员工的总薪水
任何人请帮我如何为这种情况编写查询..?
答案 0 :(得分:1)
A用表变量模拟你的情况,下面是结果
DECLARE @table TABLE (empno int,ename VARCHAR(100),salary DECIMAL(18,2),deptno int)
INSERT INTO @table SELECT 1,'shuki',450,100
INSERT INTO @table SELECT 2,'arban',500,100
INSERT INTO @table SELECT 3,'alamet',300,200
INSERT INTO @table SELECT 4,'andrea',150,200
INSERT INTO @table SELECT 5,'florim',450,200
SELECT deptno,SUM(CASE when ename LIKE 'A%' THEN salary ELSE 0 END ) SalaryEmpWithA,SUM(salary) TotalSalary FROM @table
GROUP BY deptno
输出是:
deptno SalaryEmpWithA TotalSalary
100 500.00 950.00
200 450.00 900.00