有一表工资调整表
iniwage调前工资,lastwage调后工资,day调整日期
table(iniwage,lastwage,day) 
假设天数为30天
如有数值如下
   1000   1500   6
  1500   2005   15
  2005   2500   18
  2500   3000   20现要得到工资:基本工资:1000/30*6+1500/30*9+2005/30*3+2500/30*2+3000/30*10这样sql如何实现;

解决方案 »

  1.   

    取整
    --> --> 
     
    if not object_id('Tempdb..#T') is null
    drop table #T
    Go
    Create table #T([iniwage] int,[lastwage] int,[day] int)
    Insert #T
    select 1000,1500,6 union all
    select 1500,2005,15 union all
    select 2005,2500,18 union all
    select 2500,3000,20
    Go
    with C
    as
    (select [iniwage],[DiffDay]=[day]-(select isnull(max([day]),0) from #T where [day]<t.[day]) from #T t
    union all
    select max([lastwage]),10 from #T)
    select cast(sum([iniwage]*1.0/30*[DiffDay]) as int)
    from C(4 個資料列受到影響)-----------
    2017(1 個資料列受到影響)
      

  2.   

    declare @table table(iniwage money,lastwage money,day  int) 
    declare @daynum int
     set @daynum=30 
    insert into @table
    values(1000,1500,6 )
    insert into @table
    values(1500,2005,15 )insert into @table
    values(2005,2500,18 )
    insert into @table
    values(2500,3000,20 )
    declare @money money
    declare @iniwage money,@lastwage money,@day  int,@lastday int
    set @lastday=0
    declare temp cursor for select iniwage,lastwage,day from @table order by day asc
    open temp
    fetch next from temp into @iniwage,@lastwage,@day
    while(@@fetch_status=0)
    beginset @money=isnull(@money,0)+@iniwage/@daynum*(@day-@lastday)
     
    set @lastday=@day
    fetch next from temp into @iniwage,@lastwage,@day
    end
    close temp
    deallocate tempset @money=isnull(@money,0)+@lastwage/@daynum*(@daynum-@lastday)select @money
    --select 1000/30.00*6+1500/30.00*9+2005/30.00*3+2500/30.00*2+3000/30.00*10 
      

  3.   

    --> 测试数据: @table
    declare @table table (iniwage int,lastwage int,day int)
    insert into @table
    select 1000,1500,6 union all
    select 1500,2005,15 union all
    select 2005,2500,18 union all
    select 2500,3000,20
    select sum([end]*1.0/30*[day]) from
    (select [end]=isnull(a.lastwage,b.iniwage),[day]=isnull(b.day,30)-isnull(a.day,0)
    from @table a full join @table b on a.lastwage=b.iniwage)a--结果:
    2017.166663
      

  4.   

    select sum(WAGE*Day) as 基本工资
    FROM 
    (select  iniwage/30 As WAGE ,day AS DAY from iniwage)
     AS A呵呵,试试看~~~~
      

  5.   


    Create table #T([iniwage] int,[lastwage] int,[day] int)
    Insert #T
    select 1000,1500,6 union all
    select 1500,2005,15 union all
    select 2005,2500,18 union all
    select 2500,3000,20
    Go
    with C1
    as
    (select *,rowid = row_number() over(order by iniwage) from #t
     union all
     select max(lastwage) ,0,30 - max([day]) ,199 from #t)
    select sum(a.iniwage /30.0 * (a.[day] - isnull(b.[day],0)))
    from c1 as a
    left join c1 as b
    on a.rowid = b.rowid + 1
    drop table #t
    /*
    ---------------------------------------
    2017.166663(1 行受影响)
    */
      

  6.   

    DECLARE @t TABLE ([iniwage] INT,[lastwage] INT,[day] INT)
    INSERT @t
    SELECT 1000,1500,6 UNION ALL
    SELECT 1500,2005,15 UNION ALL
    SELECT 2005,2500,18 UNION ALL
    SELECT 2500,3000,20;WITH fc AS
    (
    SELECT ROW_NUMBER() OVER (ORDER BY day) idx,* FROM @t
    )SELECT SUM(day * iniwage /30)
    FROM
    (
    SELECT a.day-ISNULL(b.day,0) day,a.iniwage
    FROM fc a
    LEFT JOIN fc b
    ON a.idx=b.idx+1
    UNION ALL
    SELECT 30-day,lastwage FROM fc a
    WHERE NOT EXISTS
    (
    SELECT 1 FROM fc WHERE idx>a.idx
    )
    ) x