表 存款存款额    已存天数    存款人(不重复)
52           201        a
10           30         b
536          8         xx
44           215       xxx
21           180       xxxx
52           181       xxxxx
0-60天  日利率是0
60-180天 日利率是0.01
大于180天 日利率是0.02现在要查4月份的利息数,比如存款人a 的4月份利息
201天-180天 = 21天,这21天的日利率是0.024月份31天,还有10天的利息,那10天的利息就在60-180的区间里面,日利率是0.01,最后把这21天的跟10天的利息加起来。

解决方案 »

  1.   

    这不是利息,是罚息,公司卖出东西收不回货款,然后根据销售的天数去惩罚。该怎么写这样的sql啊
      

  2.   

    select 存款额,已存天数,存款人,
    利息=case when 已存天数>180 then 存款额*(已存天数-180)*0.02+存款额*(180-60)*0.01
              when 已存天数 between 60 and 180 then 存款额*(已存天数-60)*0.01
              else 0
    end from 存款
      

  3.   


    declare @存款 table (存款额 int,已存天数 int,存款人 varchar(5))
    insert into @存款
    select 52,201,'a' union all
    select 10,30,'b' union all
    select 28,241,'c' union all
    select 536,8,'xx' union all
    select 44,215,'xxx' union all
    select 21,180,'xxxx' union all
    select 52,181,'xxxxx' union all
    select 78,64,'xxxyy'select 存款额,已存天数,存款人,利息=
    存款额*
    case when sign(已存天数-180)=1 then 已存天数-180 else 0 end*0.02
    +
    case when sign(已存天数-180)=-1 and sign(已存天数-60)=1 then 已存天数-60 else 0 end*0.01
    from @存款
    /*
    存款额         已存天数        存款人   利息
    ----------- ----------- ----- ---------------------------------------
    52          201         a     21.84
    10          30          b     0.00
    28          241         c     34.16
    536         8           xx    0.00
    44          215         xxx   30.80
    21          180         xxxx  0.00
    52          181         xxxxx 1.04
    78          64          xxxyy 0.04
    */
      

  4.   

    create table 存款表
    (存款额 decimal(10,2),
    已存天数 int,
    存款人 varchar(20)
    )
    insert 存款表 select 52,201,'a'
    union all select 10,30,'b'
    union all select 536,8,'xx'
    union all select 44,215,'xxx'
    union all select 52,181,'xxxx'select 存款额,已存天数,存款人,
    [利息]=(case when 已存天数<=60 then 0 
    when 已存天数<=180 then 存款额*(已存天数-60)*0.01 
    else 120*存款额*0.01+存款额*(已存天数-180)*0.02  end)
    from 存款表
    我的还得优化,学习楼上几位
      

  5.   

    表中没有时间字段,加上时间,放在where 条件后面。
      

  6.   


    declare @存款 table (存款额 int,已存天数 int,存款人 varchar(5))
    insert into @存款
    select 52,201,'a' union all
    select 10,30,'b' union all
    select 28,241,'c' union all
    select 536,8,'xx' union all
    select 44,215,'xxx' union all
    select 21,180,'xxxx' union all
    select 52,181,'xxxxx' union all
    select 78,64,'xxxyy'select 存款额,已存天数,存款人,利息=
    存款额*
    (--是不是应该加一个括号啊?
    case when sign(已存天数-180)=1 then 已存天数-180 else 0 end*0.02
    +
    case when sign(已存天数-180)=-1 and sign(已存天数-60)=1 then 已存天数-60 else 0 end*0.01
    )
    from @存款
      

  7.   

    --当月的利息,当已存天数存放的是到今天的天数时
    select 存款额,已存天数,存款人,
    利息=case when 已存天数-180>=day(getdate()) then 存款额*day(getdate())*0.02
              when 已存天数-180<day(getdate()) and 已存天数-180>=0 then 存款额*(已存天数-180)*0.02+存款额*(day(getdate())+180-已存天数)*0.01
      when 已存天数-60>=day(getdate())  then 存款额*day(getdate())*0.01
              when 已存天数-60<day(getdate()) and 已存天数-60>=0 then 存款额*(已存天数-180)*0.01
    end
    from 存款