复古活动生效日期随日期重叠而变化

时间:2010-12-27 21:34:07

标签: sql sql-server

我有一种情况需要为员工的考勤卡分配适当的费率。无论费率如何,员工都会记录他们的时间(即在记录时间时不可用)。存储费率的系统将收到每日更新,费率可以在成本核算期内的任何时间点返回(追溯)。出于报告目的,我需要能够在特定日期报告费率。请考虑以下表格:

create table #timesheet (
    txid int identity(1,1) 
  , empid int
  , work_date datetime
  , jobno varchar(16)
  , hoursamt decimal(8,2)
)

create table #payrate (
    txid int identity(1,1) 
  , empid int
  , rate money
  , eff_date datetime
  , asof_date datetime
)

insert into #timesheet ( empid, work_date, jobno, hoursamt ) values ( 1, '2010-01-01', 'A', 8 )
insert into #timesheet ( empid, work_date, jobno, hoursamt ) values ( 1, '2010-01-02', 'A', 8 )
insert into #timesheet ( empid, work_date, jobno, hoursamt ) values ( 1, '2010-01-03', 'A', 8 )
insert into #timesheet ( empid, work_date, jobno, hoursamt ) values ( 1, '2010-01-04', 'A', 8 )
insert into #timesheet ( empid, work_date, jobno, hoursamt ) values ( 2, '2010-01-01', 'B', 8 )
insert into #timesheet ( empid, work_date, jobno, hoursamt ) values ( 2, '2010-01-02', 'B', 8 )
insert into #timesheet ( empid, work_date, jobno, hoursamt ) values ( 2, '2010-01-03', 'B', 8 )
insert into #timesheet ( empid, work_date, jobno, hoursamt ) values ( 2, '2010-01-04', 'B', 8 )

insert into #payrate ( empid, rate, eff_date, asof_date ) values ( 1, 10, '2010-01-01', '2010-01-01' )      -- Starting rate
insert into #payrate ( empid, rate, eff_date, asof_date ) values ( 1, 11, '2010-01-03', '2010-01-05' )      -- Retro active
insert into #payrate ( empid, rate, eff_date, asof_date ) values ( 2, 8,  '2010-01-01', '2010-01-01' )      -- Starting rate
insert into #payrate ( empid, rate, eff_date, asof_date ) values ( 2, 9,  '2010-01-03', '2010-01-02' )      -- Retro active date
insert into #payrate ( empid, rate, eff_date, asof_date ) values ( 2, 10, '2010-01-01', '2010-01-05' )      -- Retro active date

我想要一个接受一个参数(asof_date)的查询,该参数指示费率生效的日期。给定以下“asof”日期,相同的查询将返回以下结果。请注意,追溯活动更改会在payrate表中的'asof'日期生效。假设费率在通过payrate表中的条目更改之前有效。成本=费率*小时。日期为YYYY-MM-DD格式。这些注释仅供参考,不应在结果集中返回。

declare @asof datetime
set @asof = '2010-01-01'

----------------------------------------------------
empid | work_date | jobno | hoursamt | rate | cost | Notes
----------------------------------------------------
1       2010-01-01  A       8          10     80     Rate effective the 1st  
1       2010-01-02  A       8          10     80
1       2010-01-03  A       8          10     80
1       2010-01-04  A       8          10     80
2       2010-01-01  B       8          8      64     Rate effective the 1st  
2       2010-01-02  B       8          8      64
2       2010-01-03  B       8          8      64
2       2010-01-04  B       8          8      64



declare @asof datetime
set @asof = '2010-01-03'

----------------------------------------------------
empid | work_date | jobno | hoursamt | rate | cost | Notes
----------------------------------------------------
1       2010-01-01  A       8          10     80     Rate effective the 1st  
1       2010-01-02  A       8          10     80
1       2010-01-03  A       8          10     80
1       2010-01-04  A       8          10     80
2       2010-01-01  B       8          8      64       
2       2010-01-02  B       8          8      64
2       2010-01-03  B       8          9      72     Rate effective the 3rd
2       2010-01-04  B       8          9      72


declare @asof datetime
set @asof = '2010-01-06'

----------------------------------------------------
empid | work_date | jobno | hoursamt | rate | cost | Notes
----------------------------------------------------
1       2010-01-01  A       8          10     80       
1       2010-01-02  A       8          10     80
1       2010-01-03  A       8          11     88     Rate effective the 3rd
1       2010-01-04  A       8          11     88
2       2010-01-01  B       8          10     80     Rate effective the 1st
2       2010-01-02  B       8          10     80
2       2010-01-03  B       8          10     80
2       2010-01-04  B       8          10     80

提前致谢!

1 个答案:

答案 0 :(得分:1)

您可以使用outer apply搜索特定的付费率。此示例将outer applytop 1组合在一起,以选择最近的付费率。

select  t.empid
,       t.work_date
,       t.jobno
,       t.hoursamt
,       rate.rate
,       t.hoursamt * rate.rate
from    #timesheet t
outer apply
        (
        select  top 1 *
        from    #payrate p
        where   p.empid = t.empid
                and p.asof_date <= @asof
                and p.eff_date <= t.work_date
        order by
                p.eff_date desc
        ) rate