0

请谁能告诉我为什么下面的代码不从扣除表中计算金额,然后从出勤表中的金额总和中扣除以获得净工资。在应用扣除之前,它不会将出勤表中的金额总和加倍,而不是相加和扣除,我非常感激。

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
4

1 回答 1

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
于 2012-05-13T11:07:41.870 回答