0

这是运行此语句的不同方式或使其更有效的方式的问题。

该声明旨在列出最快将离开现场的所有员工,但他们的员工今天也必须在现场。

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

如果有人想出更好的方法来写这个,请回复谢谢:)

4

2 回答 2

1

试试这个

SELECT  
    e.EmpName, 
    cs.SiteName 
FROM 
    Employee e 
INNER JOIN 

(
 SELECT  custid,empid from EmployeeAssignment ea1 where Enddate=( 
             select MIN(ea.EndDate) 
            FROM  
                 EmployeeAssignment ea 
             WHERE 
                GETDATE() BETWEEN ea.StartDate AND ea.EndDate 
                and ea.custid=ea1.custid
             )
) as ea
ON 
    e.EmpId = ea.EmpId 
INNER JOIN 
    CustomerSite cs 
ON 
    ea.CustId = cs.CustId 
于 2012-07-16T13:04:02.567 回答
0

这可能有效,也可能无效:

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
GROUP BY   
e.EmpName,
cs.SiteName
HAVING ea.EndDate = 
        (
        SELECT 
            MIN(ea.EndDate)
        FROM 
             EmployeeAssignment ea
         WHERE
            GETDATE() BETWEEN ea.StartDate AND ea.EndDate);
于 2012-07-16T13:07:12.730 回答