案例 何时在基于层级的税收系统上计算税收
Posted
技术标签:
【中文标题】案例 何时在基于层级的税收系统上计算税收【英文标题】:CASE WHEN to calculate tax on a tier based tax system 【发布时间】:2019-06-24 15:08:11 【问题描述】:我有一张用户收入表,我希望根据该收入计算他们的所得税百分比。问题是每个等级的税率不同,例如:
MinLimit| MaxLimit| TaxRate
0 | 14000 | 10.50
14001 | 48000 | 17.50
48001 | 70000 | 30.00
70001 | 1000000 | 33.00
所以如果一个人的收入是49,000,那么他们将被征税如下:
14000 * 0.1050 = 1470
34000 * 0.1750 = 5950 (34,000 is income between 14k -48k)
1000 * 0.30 = 300 (1000 is remaining income)
total = 1470 + 5950 + 300 = 7720
我在 SQL Server 2017 Express 上运行。我尝试过运行链式 CASE-WHEN 语句,即
CASE WHEN
THEN
WHEN
THEN
and so on...
但我可以弄清楚如何添加减去剩余金额的逻辑。请在下面找到我的代码。
SELECT 'emp_name' AS 'Director',
SUM(ABS([Transaction Amount])) AS 'INCOME',
CASE WHEN (SUM(ABS([Transaction Amount])) < 14000)
THEN ((SUM(ABS([Transaction Amount])) - 14000) * 0.1050)
WHEN (SUM(ABS([Transaction Amount])) > 14000 and (SUM(ABS([Transaction Amount])) < 48001))
THEN (((SUM(ABS([Transaction Amount])) - 14000) * 0.1050) - 48000) * 0.1750 end AS 'Income Tax'
FROM Transactions
编辑 1: 输入数据:
Transaction Type| PAYEE | Transaction Amount
DEBIT | DEBIT | -184.00
CREDIT | CREDIT | 4000.00
...
输出数据:
Director | INCOME | Income Tax
emp_name | 45100.00| NULL
请让我知道我哪里出错了,或者我的想法是否不正确。
【问题讨论】:
能不能写下输入数据和输出数据? @StepUp 添加输入/输出 :) 【参考方案1】:相关子查询可能是最容易阅读和理解的:
declare @t table (MinLimitExclusive int, MaxLimitInclusive int, TaxRate decimal(5,2))
insert into @t(MinLimitExclusive,MaxLimitInclusive,TaxRate) values
(0 ,14000 , 10.50),
(14000,48000 , 17.50),
(48000,70000 , 30.00),
(70000,1000000, 33.00)
declare @transactions table (Income decimal(10,2))
insert into @transactions (Income) values (49000)
select
(Income - MinLimitExclusive) * TaxRate / 100 +
(select SUM((rates2.MaxLimitInclusive - rates2.MinLimitExclusive) * rates2.TaxRate / 100)
from @t rates2 where rates2.MaxLimitInclusive <= rates.MinLimitExclusive)
from
@transactions tr
inner join
@t rates
on
tr.Income > rates.MinLimitExclusive and tr.Income <= rates.MaxLimitInclusive
当您意识到您需要做的与实际收入相关的唯一数学运算与它实际适合的范围有关时,这显着简化了 - 隐含地,所有较低税率的范围都被完整地使用了,因此您可以纯粹从税率表中计算其他税费。
我稍微更改了您的费率数据,以使计算简单明了,不需要对+/-1
进行大量调整。
【讨论】:
感谢@Damien --> 以您的代码为灵感,并稍作修改即可使用。非常感激! :)【参考方案2】:我建议您从 1 而不是 0 的 MinLimit 开始。其余的计算很简单:
declare @taxslabs table (minlimit int, maxlimit int, taxrate decimal(18, 2));
insert into @taxslabs values
(1, 14000, 10.50),
(14001, 48000, 17.50),
(48001, 70000, 30.00),
(70001, 1000000, 33.00);
select persons.*, taxslabs.*, taxableamount, taxableamount * taxrate / 100 as taxamount
from (values
(1, 49000),
(2, 70000),
(3, 70001)
) as persons(id, income)
cross join @taxslabs as taxslabs
cross apply (select case when income <= maxlimit then income else maxlimit end - minlimit + 1) as ca(taxableamount)
where minlimit <= income
您可以将此查询放在子查询中,并使用GROUP BY ... SUM()
或SUM() OVER (PARTITION BY)
计算税款总和。
样本输出:
| id | income | minlimit | maxlimit | taxrate | taxableamount | taxamount |
|----|--------|----------|----------|---------|---------------|------------------|
| 1 | 49000 | 1 | 14000 | 10.50 | 14000 | 1470.000000 |
| 1 | 49000 | 14001 | 48000 | 17.50 | 34000 | 5950.000000 |
| 1 | 49000 | 48001 | 70000 | 30.00 | 1000 | 300.000000 |
| 2 | 70000 | 1 | 14000 | 10.50 | 14000 | 1470.000000 |
| 2 | 70000 | 14001 | 48000 | 17.50 | 34000 | 5950.000000 |
| 2 | 70000 | 48001 | 70000 | 30.00 | 22000 | 6600.000000 |
| 3 | 70001 | 1 | 14000 | 10.50 | 14000 | 1470.000000 |
| 3 | 70001 | 14001 | 48000 | 17.50 | 34000 | 5950.000000 |
| 3 | 70001 | 48001 | 70000 | 30.00 | 22000 | 6600.000000 |
| 3 | 70001 | 70001 | 1000000 | 33.00 | 1 | 0.330000 |
【讨论】:
【参考方案3】:我认为在事务表上使用 group by 并加入到税率税表的相关查询可能会被关闭,但结果除外:
CREATE TABLE #Transaction
(
tID int PRIMARY KEY,
tIdUser varchar(50),
Amount decimal(9,3)
);
CREATE TABLE #RefTaxe
(
pID int PRIMARY KEY,
minLimit int,
maxLImit int,
rate decimal(9,3)
);
INSERT INTO #Transaction
SELECT 1, 'User1', 1259.3
UNION
SELECT 2, 'User1', 10259.3
UNION
SELECT 3, 'User3', 30581.3
UNION
SELECT 4, 'User2', 75000.36
UNION
SELECT 5, 'User2', 15000.36
UNION
SELECT 6, 'User4', 45000.36
UNION
SELECT 7, 'User4', 5000.36
INSERT INTO #RefTaxe
select 1,0,14000,10.50
UNION
SELECT 2,14001,48000,17.50
UNION
SELECT 3,48001,70000,30.00
UNION
SELECT 4,70001,1000000,33.00
-- SELECT * FROM #Transaction
-- SELECT * FROM #RefTaxe
-- SELECT tIdUser,SUM(AMOUNT) as SumAmount, CAST(FLOOR(SUM(AMOUNT))as int) as SumAsInt FROM #Transaction GROUP BY tIdUser
/***/
-- Perform select
/***/
SELECT tIdUser, SumAmount as 'DetaxedAmount' ,SumAmount * (rate/100) as TaxOfAmount, SumAmount+ SumAmount * (rate/100) as TaxedAmount
FROM #RefTaxe RT
JOIN (
SELECT tIdUser,SUM(AMOUNT) as SumAmount, CAST(FLOOR(SUM(AMOUNT))as int) as SumAsInt
FROM #Transaction GROUP BY tIdUser
) AS GroupedTR ON RT.minLimit <= SumAsInt AND RT.maxLImit >= SumAsInt
/***/
DROP TABLE #Transaction
DROP TABLE #RefTaxe
结果输出:
tIdUser DetaxedAmount TaxOfAmount TaxedAmount
User1 11518.600 1209.453000 12728.053
User2 90000.720 29700.237600 119700.958
User3 30581.300 5351.727500 35933.028
User4 50000.720 15000.216000 65000.936
【讨论】:
以上是关于案例 何时在基于层级的税收系统上计算税收的主要内容,如果未能解决你的问题,请参考以下文章