这是运行此语句的不同方式或使其更有效的方式的问题。
该声明旨在列出最快将离开现场的所有员工,但他们的员工今天也必须在现场。
SELECT
e.EmpName,
cs.SiteName
FROM
Employee e
INNER JOIN
EmployeeAssignment ea
ON
e.EmpId = ea.EmpId
INNER JOIN
CustomerSite cs
ON
ea.CustId = cs.CustId
WHERE
e.EmpId IN
(
SELECT
ea.EmpId
FROM
EmployeeAssignment ea
WHERE
ea.EndDate IN
(
SELECT
MIN(ea.EndDate)
FROM
EmployeeAssignment ea
WHERE
GETDATE() BETWEEN ea.StartDate AND ea.EndDate
GROUP BY
CustId
));
如果有人想出更好的方法来写这个,请回复谢谢:)