请有人告诉我为什么以下代码不会从扣除表中扣除金额,然后从出勤表中的金额中扣除以获得净工资。在应用扣除之前,它不是总结和扣除,而是将出勤金额的总和加倍,我非常感激。
select sum(attendance.amount) - max(deduction.amount)
from attendance
join deduction on attendance.staffid = deduction.staffid
where attendance.staffid = some_staffid
and month(attendance.date) = some_month
and month(deduction.date_approved) = some_month
答案 0 :(得分:0)
只是一个疯狂的猜测 - 每次出勤都有2个扣除记录=>联接将复制出勤表中的金额=>总和将对这些重复的记录进行求和
简单的解决方案(但表现不是很好,我很害怕)将是:
select sum(a) - max_d
from (
select attendance.amount a, deduction.amount d, max(deduction.amount) max_d
from attendance
join deduction on attendance.staffid = deduction.staffid
where attendance.staffid = some_staffid
and month(attendance.date) = some_month
and month(deduction.date_approved) = some_month
)
where d = max_d