1. select * from EMPLOYEE where Salary>(select avg(Salary) from EMPLOYEE group by Dnumber)
2. select * from EMPLOYEE where ESSN in (select ESSN from WORKS_ON group by Pnumber,ESSN having Count(1)>=3)
3. -- 每个部门的所有职工在每个项目中的工作总时数
Create View DeptPro
AS
select Pnumber, Dnumber, Dname, sum(Hours) Hours from (
select A.Pnumber, A.Hours, C.Dnumber, C.Dname
from WORKS_ON A
Inner join EMPLOYEE B on A.ESSN=B.ESSN
Inner join DEPARTMENT C on B.Dnumber=C.Dnumber
) Tbl group by Pnumber,Dnumber,Dname
--工作时数最多的部门名称。
select Max(Hours) Hours, Dnumber, Dname from DeptPro
where Pnumber in (select Pnumber from PROJECT where Pname='Middleware')
group by Dnumber, Dname
(1)
SELECT Ename, Dnumber, Salary FROM EMPLOYEE
JOIN
(SELECT Dnumber, avg(Salary) AS Asalary FROM EMPLOYEE
GROUP BY Dnumber) A
ON Dnumber = A.Dnumber
WHERE Salary > A.Asalary
ORDER BY Dnumber
(2)
SELECT Ename FROM EMPLOYEE
JOIN
(SELECT ESSN, COUNT(Pnumber) AS Pcount FROM WORKS_ON
GROUP BY ESSN
HAVING COUNT(Pnumber) > 3) A --未考虑同一个职工在不同时期参加同一个项目情况
ON ESSN = A.ESSN
(3)
CREATE VIEW DEPTPRO AS
(SELECT Dnumber, Pnumber, SUM(Hours) AS Thours FROM WORKS_ON
GROUP BY Dnumber, Pnumber); --每个部门的所有职工在每个项目中的工作总时数
SELECT A.Pname FROM DEPTPRO
JOIN DEPARTMENT A
ON Dnumber = A.Dnumber
JOIN PROJECT B
ON Pnumber = B.Pnumber
WHERE B.Pname = 'Middleware'
AND Thours = MAX(Thours);--在项目'Middleware'中工作时数最多的部门名称