考虑公司数据库的以下模式(主键以粗体显示)。
Employee (**EmployeeID**, Name, Office, Email, Sex, Salary, DepartmentID)
Department (**DepartmentID**, DepartmentName)
Project (**ProjectID**, DepartmentID, Duration)
EmployeeProject (**EmployeeID, ProjectID**, WorkHours)
使用SINGLE QUERY语句查找总工时小于第二长的项目。 请勿尝试使用top或limit。
以下是我的解决方案,它相当丑陋且效率低下。
select projectid
from (
select projectid, sum(workhours) as hours
from employeeproject ep
group by projectid
) sum
where hours = (
select max(hours)
from (
select projectid, hours
from (
select projectid, sum(workhours) as hours
from employeeproject ep
group by projectid
) sum
where hours < (
select max(hours)
from (
select projectid, sum(workhours) as hours
from employeeproject ep
group by projectid
) sum
)
) sec
)
答案 0 :(得分:0)
由于这似乎是一个家庭作业问题,因此根据网站政策提供完整答案: https://meta.stackexchange.com/a/10812
然而,看起来我们被允许指向正确的方向。 :)
尝试将问题分解为步骤:
TOP
和ORDER BY
运营商。ORDER BY
可以按升序或降序运行。如何将这些步骤组合成单个查询应该是相当明显的。
答案 1 :(得分:0)
选择ProjectID 来自EmployeeProject 其中WORKHOURS =(选择MAX(WORKHOURS) 来自EmployeeProject 工作时间<( 从EmployeeProject中选择MAX(WORKHOURS) ) )