日期:2014-05-18  浏览次数:20369 次

面试题,税率计算问题,要求可以用程序或者存储过程实现
根据用户收入 Income 返回他所要交的税
最好用存储过程实现,下面只是个模拟,不一定与现实对应。
级别 起征点 超过 不超过 税率
rank baseIncome overmin overmax taxs
  1 2000 0 1000 5%
  2 2000 1000 3000 10%
  3 2000 3000 6000 15%
  4 2000 6000 10000 20%
  5 2000 10000 15000 25%
如上表,程序最好写活,就是说 baseIncome overmin overmax taxs 在改变的情况下程序不用修改
谢谢,那技术给我半个小时。没搞定。在这里请教高人

------解决方案--------------------
case when即可.
------解决方案--------------------
建个税率表

查表计算就可以

------解决方案--------------------
between and .>
------解决方案--------------------
SQL code

create table tb(rank int identity(1,1),baseIncome int,overmin int,overmax int,taxs varchar(10))
insert into tb select 2000,0,1000,'5%'
insert into tb select 2000,1000,3000,'10%'
insert into tb select 2000,3000,6000,'15%'
insert into tb select 2000,6000,10000,'20%'
insert into tb select 2000,10000,15000,'25%'

create proc wsp
@Income  money
as
select 所要交的税=(@Income-baseIncome)*cast(replace(taxs,'%','') as numeric(5,2))/100
from tb
where @Income-baseIncome between overmin and overmax-1 

exec wsp 5000
--结果:450

------解决方案--------------------
SQL code

create table cTaxRate
(
minV decimal(18,2),  
maxV decimal(18,2),
TaxRate decimal(18,2),  --税率
Allowance decimal(18,2) --速算扣除数
)

create function cFunctionTax(@Amount decimal(18,2))
returns decimal(18,2)
as
Begin
    declare @taxV decimal(18,2),
            @taxBase decimal(18,2)

    select @taxBase=2000
--  select @taxBase=字段 from 表
    select @taxV=(@Amount-@taxBase)*TaxRate-Allowance
    from cTaxRate
    where (@Amount-@TaxBase)>=minV
        and (@Amount-@TaxBase)<maxV
    
    return @TaxV

End

------解决方案--------------------
SQL code

create table 税率表  (
rank        int ,
baseIncome  numeric(18,2) , 
overmin     numeric(18,2),
overmax     numeric(18,2) , 
taxs       numeric(18,6)
)

insert 税率表 select
    1,             2000,                   0    ,           1000 ,         0.05
union all select 
    2,             2000,                   1000 ,        3000    ,         0.1
union all select 
    3,             2000,                   3000 ,        6000    ,         0.15
union all select 
    4,             2000,                   6000 ,        10000   ,        0.2
union all select 
    5,             2000,                   10000,       15000    ,       0.25

go

create proc pr_计算税金
@income numeric(18,2)
as

select sum(case when @income>baseIncome+overmax then overmax-overmin else @income-baseIncome-overmin end *taxs) as 税金
from 税率表
where baseIncome+overmin<=@income

go

exec pr_计算税金 2500
--25
exec pr_计算税金 3500
--100
exec pr_计算税金 9500
--1000

exec pr_计算税金 12000
--1500

------解决方案--------------------
SQL code
create table #tab (id int identity(1,1),baseincome int,overmin int,overmax int,taxs float)
insert into #tab values(2000,0,1000,0.05)
insert into #tab values(2000,1000,3000,0.1)
insert into #tab values(2000,3000,6000,0.15)
insert into #tab values(2000,6000,10000,0.2)
insert into #tab values(2000,10000,15000,0.25)


create proc #tab_p (@money float)
as
begin
        
select sum(case when @money-(baseincome+overmin)>overmax then overmax when @money-(baseincome+overmin)<0 then 0 else @money-(baseincome+overmin) end*taxs)from #tab  
end
exec #tab_p 4500


200