2

我正在尝试加入你的表,并且有一个 Sum 值,但它没有显示任何错误,但它的值是错误的......但是如果我加入两个表,它可以正常工作。

查询是

SELECT  tblEmployeeInfo.EmpCode, 
        SUM(tblLeave.lvDay) AS Total, 
        tblAtData.card_no

FROM    tblEmployeeInfo 
INNER JOIN tblLeave ON tblEmployeeInfo.EmpCode = tblLeave.lvEmpId 
INNER JOIN tblAtData ON tblEmployeeInfo.EmpCardID = tblAtData.card_no

WHERE  (tblLeave.lvMonth = @month)

GROUP BY tblEmployeeInfo.EmpCode, tblAtData.card_no
4

2 回答 2

1

您应该在子查询中计算 SUM,tblLeave然后再 JOIN 表:

  SELECT   tblEmployeeInfo.EmpCode, 
            tblLeave.SUMlvDays AS Total, 
            tblAtData.card_no

    FROM    tblEmployeeInfo 
    INNER JOIN 
        (select lvEmpId, SUM(lvDay) as SUMlvDays 
                    from  tblLeave 
          WHERE  (tblLeave.lvMonth = @month)
         group by lvEmpId )  tblLeave

           ON   tblEmployeeInfo.EmpCode = tblLeave.lvEmpId 

    INNER JOIN tblAtData ON tblEmployeeInfo.EmpCardID = tblAtData.card_no
    GROUP BY tblEmployeeInfo.EmpCode, tblAtData.card_no
于 2013-05-07T11:30:09.667 回答
0
SELECT   tblEmployeeInfo.EmpCode, 
        tblLeave.SUMlvDays AS Total, 
        tblAtData.card_no

FROM    tblEmployeeInfo 
INNER JOIN 
    (select lvEmpId, SUM(lvDay) as SUMlvDays 
                from  tblLeave 
      WHERE  (tblLeave.lvMonth = @month)
     group by lvEmpId )  tblLeave

       ON   tblEmployeeInfo.EmpCode = tblLeave.lvEmpId 

INNER JOIN tblAtData ON tblEmployeeInfo.EmpCardID = tblAtData.card_no
GROUP BY tblEmployeeInfo.EmpCode, tblAtData.card_no,tblLeave.SUMlvDays
于 2013-05-07T12:04:40.410 回答