以下查询的结果是什么?

时间:2016-01-27 12:01:28

标签: sql

enter image description here

我试过以下查询...

select d.deptID, max(tt.total)
from dept d,
(select d.deptID, d.deptName, sum(days) as total
from vacation v, employee e, dept d
where v.empId = e.empID
and d.deptID = e.deptID
group by d.deptID, d.deptName) tt
where d.deptID = tt.deptID
group by d.deptName;
--having  max(tt.total);

3 个答案:

答案 0 :(得分:2)

尝试使用限制,因为您的内部查询已经进行了计算。

select TOP 1 * from (
select d.deptID, d.deptName, sum(days) as total
from vacation v, employee e, dept d
where v.empId = e.empID
and d.deptID = e.deptID
group by d.deptID, d.deptName)
order by total desc;

取决于您正在使用的dbms ..这适用于mysql

在oracle中使用rownum = 1

在sql server中使用SELECT TOP 1 *

答案 1 :(得分:1)

使用顶部:

Select top 1 with ties * from
(Select D.DepartmentName, sum(V.Days) as SumDays
from Vacations V
inner join Employee E on E.EmployeeID=V.EmployeeID
inner join Department D on D.DepartmentID=E.DepartmentID
group by D.DepartmentName)SumDays
Order by SumDays desc

答案 2 :(得分:1)

试试这个,

   SELECT TOP 1 d.departmentname,
             Sum(v.days) AS vacations
FROM   employee emp
       INNER JOIN department d
               ON d.departmentid = emp.departmentid
       INNER JOIN vacations v
               ON v.employeeid = emp.employeeid
GROUP  BY d.departmentname
ORDER  BY 2 DESC