Sql 以下查询的结果是什么?

Sql 以下查询的结果是什么?,sql,Sql,我尝试了以下查询 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 gro

我尝试了以下查询

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);

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

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*

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

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中,使用TOP选择TOP 1*

,使用TOP:

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
使用Top:

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
试着这样,

   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
试着这样,

   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

我删除了无关的数据库标记。我删除了无关的数据库标记。如果使用SQL Server,请替换“选择*自…”。。。限制1,从…@anonymous中选择top 1*您正在使用的dbms?确定。知道了。拼写错误。从选择d.departmentid、d.departmentname、sumdays作为假期v、员工e、部门d的总计中选择前1个*,其中v.employeeid=e.employeeid和d.departmentid=e.departmentid按d.departmentid分组,d.departmentname按总计描述排序;为什么它不适用于内部查询中的ORDER BY?-ORDER BY不适用于子查询-您需要添加关系查看我的答案,因为此查询只返回一行,即使有多个部门共享相同的最大假期。如果使用SQL Server,请替换“选择*自…”。。。限制1,从…@anonymous中选择top 1*您正在使用的dbms?确定。知道了。拼写错误。从选择d.departmentid、d.departmentname、sumdays作为假期v、员工e、部门d的总计中选择前1个*,其中v.employeeid=e.employeeid和d.departmentid=e.departmentid按d.departmentid分组,d.departmentname按总计描述排序;为什么它不适用于内部查询中的ORDER BY?-ORDER BY不适用于子查询-您需要添加关系查看我的答案,因为此查询只返回一行,即使有多个部门共享相同的最大假期。正如@sagi所说,这将取决于您的RDBMS。正如@sagi所说,这将取决于您的RDBMS。选择TOP 1 d.部门名称,d.departmentid=emp.departmentid=emp.departmentid内部加入休假v.employeeid=emp.employeeid组按d.departmentid,d.departmentname按2DESC@anonymous:我已经更新了我的答案。请立即选中按d.departmentid=emp.departmentid=emp.departmentid内部加入休假v ON v.employeeid=emp.employeeid按d.departmentid从员工emp内部加入部门d中选择前1个d.departmentname,Sumv.days作为休假,d.部门名称订单号2DESC@anonymous:我已经更新了我的答案。请现在检查