按输出限制组

时间:2013-11-11 08:06:43

标签: mysql sql

在我的数据库中,我有不同的部门,分配给他们不同的人。我想给每个部门的所有员工加10%的加薪,然后加上它们,这样我就可以得到每个部门的总薪资值。但是,不应列出那些至少有一个名字以'ce'结尾的员工的部门。我有研究,管理和总部部门。只有研究和行政部门应该显示各自的总薪资值。

SELECT
dname
, SUM(1.1*salary) AS total_salary 
FROM
employee INNER JOIN department
ON dnumber=dno
WHERE lname NOT LIKE '%ce'
GROUP BY dname
ORDER BY total_salary DESC

这就是我想要的,除了列出所有部门。我怎样才能修改这些语句,以便只显示正确的部门?

2 个答案:

答案 0 :(得分:1)

SELECT dname,
SUM(1.1 * salary) AS total_salary
FROM employee
inner join department
ON dnumber = dno
WHERE dname NOT IN (SELECT dname
FROM departments
INNER JOIN employees
ON dnumber = dno
AND lname NOT LIKE '%ce')
GROUP BY dname
ORDER BY total_salary DESC

答案 1 :(得分:0)

SELECT d2.dname as dept_name, SUM(1.1*e2.salary) AS total_salary 
FROM employee e2
INNER JOIN department d2 ON e2.dnumber=d2.dno and d2.dname not like (
    select distinct(d.dname) as dept_name2 from employees e left join department d on e.dnumber=d.dno where e.lname like '%ce'
    ) as temp
WHERE e2.lname NOT LIKE '%ce'
GROUP BY dept_name
ORDER BY total_salary DESC

<强>假设:
1)department_namedepartment表中代表其名称的列 2)ResearchAdministration是您想要的部门的确切名称