个税计算
员工收入表driver_income有如下字段:
代码语言:javascript复制月份 month
员工id driver
收入 amount
税率表tax_rate有如下字段:
代码语言:javascript复制月份 month
收入区间下限 min_amount
收入区间上限 max_amount
比例 rate
注:每个月份有不同区段的税率,例子如下:
代码语言:javascript复制month min_amount max_amount rate
202102 1万 1.5万 0.6
202102 1.5万 2万 0.7
计算:员工每个月应交税额
month,driver,tax
代码语言:javascript复制select
a.driver,
a.month,
sum(case when b.max_amount > a.amount and a.amount > b.min_amount then a.amount-b.min_amount when a.amount < b.min_amount then 0 else b.max_amount - b.min_amount end * b.rate)
from
driver_income a
left join
tax_rate b
on a.month = b.month
group by 1,2
写sql时数据推演,供参考:
month,driver,amount,min_amount,max_amount,rate tax
202102 1 2万 1万, 1.5万,0.6 (1.5万-1万)*0.6
202102 1 2万 1.5万, 2万,0.7 (2万-1.5万)*0.7
202102 2 1.7万 1.5万, 2万,0.7 (1.7万-1.5万)*0.7
202102 2 1.7万 1万, 1.5万,0.5 (1.5万-1万)*0.5
202102 2 1.7万 2万, 3万,0.5 0*0.5