我正在尝试加入这些表并且有一个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
答案 0 :(得分:1)
您应该在tblLeave
的子查询中计算SUM,然后在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
答案 1 :(得分: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