计算扣税的问题,贴上我的代码,请教如何改为能过换算表计算(我的代码是完全写在代码中的,可维护性差)--计算出应纳税所得[应纳税所得=税前工资-个税起征点]
update gz set 应纳税所得 = case when (税前工资 - 个税起征点) <= 0 then 0
when (税前工资 - 个税起征点) >0 then (税前工资 - 个税起征点)
end
where 月份 = @yuefen and 批次=1--算扣税比例[写入扣税比例主要为了汇总用]
update gz
set 扣税比例 = case when 应纳税所得 <= 0 then '0'
when 应纳税所得 > 0 and 应纳税所得 <= 500 then '5%'
when 应纳税所得 > 500 and 应纳税所得 <= 2000 then '10%-25'
when 应纳税所得 > 2000 and 应纳税所得 <= 5000 then '15%-125'
when 应纳税所得 > 5000 and 应纳税所得 <= 20000 then '20%-375'
when 应纳税所得 > 20000 and 应纳税所得 <= 40000 then '25%-1375'
when 应纳税所得 > 40000 and 应纳税所得 <= 60000 then '30%-3375'
when 应纳税所得 > 60000 and 应纳税所得 <= 80000 then '35%-6375'
when 应纳税所得 > 80000 and 应纳税所得 <= 100000 then '40%-10375'
else '45%-15375'
end
where 月份 = @yuefen and 批次=1--算月税额:
update gz
set 月税 = case when 扣税比例 = '0' then 0
when 扣税比例 = '5%' then 应纳税所得 * 0.05
when 扣税比例 = '10%-25' then 应纳税所得 * 0.1 - 25
when 扣税比例 = '15%-125' then 应纳税所得 * 0.15 - 125
when 扣税比例 = '20%-375' then 应纳税所得 * 0.2 - 375
when 扣税比例 = '25%-1375' then 应纳税所得 * 0.25 - 1375
when 扣税比例 = '30%-3375' then 应纳税所得 * 0.3 - 3375
when 扣税比例 = '35%-6375' then 应纳税所得 * 0.35 - 6375
when 扣税比例 = '40%-10375' then 应纳税所得 * 0.4 - 10375
when 扣税比例 = '45%-15375' then 应纳税所得 * 0.45 - 15375
end
where 月份 = @yuefen and 批次=1
我现在的代码可以实现功能,但是维护不方便,因为如果扣税规则改变,就要改代码,
我想在数据库中增加一个换算表,将对应的规则写在表中,今后如果规则改变,直接改动表就行了,可是我不知如何做。
比如0-500元对应的是5%
比如员工的应纳税所得是430元,我如何对应到5%呢?
0 --> 500 是一个范围,如何通过一个范围来判断呢?
update gz set 应纳税所得 = case when (税前工资 - 个税起征点) <= 0 then 0
when (税前工资 - 个税起征点) >0 then (税前工资 - 个税起征点)
end
where 月份 = @yuefen and 批次=1--算扣税比例[写入扣税比例主要为了汇总用]
update gz
set 扣税比例 = case when 应纳税所得 <= 0 then '0'
when 应纳税所得 > 0 and 应纳税所得 <= 500 then '5%'
when 应纳税所得 > 500 and 应纳税所得 <= 2000 then '10%-25'
when 应纳税所得 > 2000 and 应纳税所得 <= 5000 then '15%-125'
when 应纳税所得 > 5000 and 应纳税所得 <= 20000 then '20%-375'
when 应纳税所得 > 20000 and 应纳税所得 <= 40000 then '25%-1375'
when 应纳税所得 > 40000 and 应纳税所得 <= 60000 then '30%-3375'
when 应纳税所得 > 60000 and 应纳税所得 <= 80000 then '35%-6375'
when 应纳税所得 > 80000 and 应纳税所得 <= 100000 then '40%-10375'
else '45%-15375'
end
where 月份 = @yuefen and 批次=1--算月税额:
update gz
set 月税 = case when 扣税比例 = '0' then 0
when 扣税比例 = '5%' then 应纳税所得 * 0.05
when 扣税比例 = '10%-25' then 应纳税所得 * 0.1 - 25
when 扣税比例 = '15%-125' then 应纳税所得 * 0.15 - 125
when 扣税比例 = '20%-375' then 应纳税所得 * 0.2 - 375
when 扣税比例 = '25%-1375' then 应纳税所得 * 0.25 - 1375
when 扣税比例 = '30%-3375' then 应纳税所得 * 0.3 - 3375
when 扣税比例 = '35%-6375' then 应纳税所得 * 0.35 - 6375
when 扣税比例 = '40%-10375' then 应纳税所得 * 0.4 - 10375
when 扣税比例 = '45%-15375' then 应纳税所得 * 0.45 - 15375
end
where 月份 = @yuefen and 批次=1
我现在的代码可以实现功能,但是维护不方便,因为如果扣税规则改变,就要改代码,
我想在数据库中增加一个换算表,将对应的规则写在表中,今后如果规则改变,直接改动表就行了,可是我不知如何做。
比如0-500元对应的是5%
比如员工的应纳税所得是430元,我如何对应到5%呢?
0 --> 500 是一个范围,如何通过一个范围来判断呢?
0 --> 500 即 下限为0,上限为500 用between and 来判断
if object_id('[换算表]') is not null drop table [换算表]
go
create table [换算表]([下限] int,[上限] int,[比例] varchar(6))
insert [换算表]
select 0,500,'5%' union all
select 500,2000,'10%-25'
if object_id('[员工号]') is not null drop table [员工号]
go
create table [员工号]([员工号] varchar(3),[应纳税所得] int)
insert [员工号]
select '001',766 union all
select '002',200 union all
select '003',433
---查询---
select
a.*,
b.比例 as 扣税比例
from [员工号] a
left join [换算表] b
on a.应纳税所得 between b.[下限] and b.[上限]---结果---
员工号 应纳税所得 扣税比例
---- ----------- ------
001 766 10%-25
002 200 5%
003 433 5%(所影响的行数为 3 行)
级别 扣税比例 下限 上限 乘项 减项
---- -------------------- -------------------- -------------------- -------------------- --------------------
1 5% .00 500.00 .05 .00
2 10%-25 500.00 2000.00 .10 25.00
3 15%-125 2000.00 5000.00 .15 125.00
4 20%-375 5000.00 20000.00 .20 375.00
5 25%-1375 20000.00 40000.00 .25 1375.00
6 30%-3375 40000.00 60000.00 .30 3375.00
7 35%-6375 60000.00 80000.00 .35 6375.00
8 40%-10375 80000.00 100000.00 .40 10375.00
9 45%-15375 100000.00 NULL .45 15375.00关键是第9个级别如何判断呢?只有下限,没有上限呀。
500.00 2000.00 用between and的话,当条件为500时,两个条件都满足
当应纳税所得为 10万元以下时,不管上限为多少,扣税比例都是
45%-15375.此时用 between 如何判断呢?即上限是一个无限大的值。
当然了,我可以将上限设定为一个很大的值,但必竟不是好的办法。
我想从语法上解决这个问题。update gz
set 扣税比例=isnull(b.扣税比例,'0'),月税=isnull(a.应纳税所得,0)*isnull(b.乘项,0)-isnull(b.减项,0)
from gz a left join tax_month b on (a.应纳税所得>b.下限 and a.应纳税所得<=b.上限)
where a.月份=@yuefen and a.批次=1以下是我写的代码,就是不能处理上限为无穷大的情况。
如果只有一条这样的记录,把它筛选出来,没有上限只判断大于的情况,然后用union all合并
即
select * from tb where 上限 is not null
union all
select * from tb where 上限 is null
这种形式
--把它倒过来呢?--算扣税比例[写入扣税比例主要为了汇总用]
update gz
set 扣税比例 = case when 应纳税所得 > 100000 then '45%-15375'
when 应纳税所得 > 80000 and 应纳税所得 <= 100000 then '40%-10375'
when 应纳税所得 > 60000 and 应纳税所得 <= 80000 then '35%-6375'
when 应纳税所得 > 40000 and 应纳税所得 <= 60000 then '30%-3375'
when 应纳税所得 > 20000 and 应纳税所得 <= 40000 then '25%-1375'
when 应纳税所得 > 5000 and 应纳税所得 <= 20000 then '20%-375'
when 应纳税所得 > 2000 and 应纳税所得 <= 5000 then '15%-125'
when 应纳税所得 > 500 and 应纳税所得 <= 2000 then '10%-25'
when 应纳税所得 > 0 and 应纳税所得 <= 500 then '5%'
else '0'
endwhere 月份 = @yuefen and 批次=1--算月税额:
update gz
set 月税 = case when 扣税比例 = '45%-15375' then 应纳税所得 * 0.45 - 15375
when 扣税比例 = '40%-10375' then 应纳税所得 * 0.4 - 10375
when 扣税比例 = '35%-6375' then 应纳税所得 * 0.35 - 6375
when 扣税比例 = '30%-3375' then 应纳税所得 * 0.3 - 3375
when 扣税比例 = '25%-1375' then 应纳税所得 * 0.25 - 1375
when 扣税比例 = '20%-375' then 应纳税所得 * 0.2 - 375
when 扣税比例 = '15%-125' then 应纳税所得 * 0.15 - 125
when 扣税比例 = '10%-25' then 应纳税所得 * 0.1 - 25
when 扣税比例 = '5%' then 应纳税所得 * 0.05
when 扣税比例 = '0' then 0
end
where 月份 = @yuefen and 批次=1
when 扣税比例 = '40%-10375' then 应纳税所得 * 0.4 - 10375
when 扣税比例 = '35%-6375' then 应纳税所得 * 0.35 - 6375
when 扣税比例 = '30%-3375' then 应纳税所得 * 0.3 - 3375
when 扣税比例 = '25%-1375' then 应纳税所得 * 0.25 - 1375
when 扣税比例 = '20%-375' then 应纳税所得 * 0.2 - 375
when 扣税比例 = '15%-125' then 应纳税所得 * 0.15 - 125
when 扣税比例 = '10%-25' then 应纳税所得 * 0.1 - 25
when 扣税比例 = '5%' then 应纳税所得 * 0.05
when 扣税比例 = '0' then 0
end那两列不是可以设在表里吗?可以修改参数,达到所要的结果.
9 45%-15375 100000.00 NULL .45 15375.00 -----------------------------------
当应纳税所得为 10万元以下时,不管上限为多少,扣税比例都是
45%-15375. 此时用 between 如何判断呢?即上限是一个无限大的值。
当然了,我可以将上限设定为一个很大的值,但必竟不是好的办法。
我想从语法上解决这个问题。 update gz
set 扣税比例=isnull(b.扣税比例,'0'),月税=isnull(a.应纳税所得,0)*isnull(b.乘项,0)-isnull(b.减项,0)
from gz a left join tax_month b on (a.应纳税所得>b.下限 and a.应纳税所得 <=b.上限)
where a.月份=@yuefen and a.批次=1 以下是我写的代码,就是不能处理上限为无穷大的情况。
set 扣税比例=isnull(b.扣税比例,'0'),月税=isnull(a.应纳税所得,0)*isnull(b.乘项,0)-isnull(b.减项,0)
from gz a left join tax_month b on (a.应纳税所得>b.下限 and a.应纳税所得 <=b.上限)
where a.月份=@yuefen and a.批次=1 这条语句是成功的,就是不适用于当上限为 无穷大 的情况,
1.我在换算表中如何表示“无穷大”这个含义
2.我的上述代码如何修改呢?
set 扣税比例=isnull(b.扣税比例,'0'),月税=isnull(a.应纳税所得,0)*isnull(b.乘项,0)-isnull(b.减项,0)
from gz a left join tax_month b on (b.上限 is not null and a.应纳税所得>b.下限 and a.应纳税所得 <=b.上限) or (b.上限 is null and a.应纳税所得>b.下限)
where a.月份=@yuefen and a.批次=1