2016-09-13 44 views
0

我有点问题围绕着这个不断变化的维度的逻辑。我想将以下两张表联系起来。我需要根据ID和生效日期将成本期间事实表与成本维度相匹配。使用日期字段来匹配SQL查询

正如您所看到的 - 如果月份和年份字段大于其关联成本维度的生效日期,则应采用该值。一旦将新的生效日期输入到维度中,它应该将该值用于大于所述日期的任何时间段。

编辑:我对缺乏细节表示歉意,但成本维度实际上会有一个唯一的索引值,并且匹配引用的变化字段为资源,项目,成本。我试图将您提供的查询与我的字段进行匹配,但是我收到了错误的输出。

FYI:命名约定的变化:EngagementId是标识,资源是ConsultantId,和项目是专案编号

我已经改变了下面的图片,这里是我的查询

 ,_cte(HoursWorked, HoursBilled, Month, Year, EngagementId, ConsultantId, ConsultantName, ProjectId, ProjectName, ProjectRetainer, RoleId, Role, Rate, ConsultantRetainer, Salary, amount, EffectiveDate) 
    as 
    (
    select sum(t.Duration), 0, Month(t.StartDate), Year(t.StartDate), t.EngagementId, c.ConsultantId, c.ConsultantName, c.ProjectId, c.ProjectName, c.ProjectRetainer, c.RoleId, c.Role, c.Rate, c.ConsultantRetainer, 
c.Salary, 0, c.EffectiveDate 
     from timesheet t 
     left join Engagement c on t.EngagementId = c.EngagementId and Month(c.EffectiveDate) = Month(t.EndDate) and Year(c.EffectiveDate) = Year(t.EndDate) 
     group by Month(t.StartDate), Year(t.StartDate), t.EngagementId, c.ConsultantName, c.ConsultantId, c.ProjectId, c.ProjectName, c.ProjectRetainer, c.RoleId, c.Role, c.Rate, c.ConsultantRetainer, 
c.Salary, c.EffectiveDate 

    ) 
    select * from _cte where EffectiveDate is not null 
    union 
    select _cte.HoursWorked, _cte.HoursBilled, _cte.Month, _cte.Year, _cte.EngagementId, _cte.ConsultantId, _cte.ConsultantName, _cte.ProjectId, _Cte.ProjectName, _cte.ProjectRetainer, _cte.RoleId, _cte.Role, sub.Rate, _cte.ConsultantRetainer,_cte.Salary, _cte.amount, sub.EffectiveDate 
     from _cte 
     outer apply (
       select top 1 EffectiveDate, Rate 
       from Engagement e 
       where e.ConsultantId = _cte.ConsultantId and e.ProjectId = _cte.ProjectId and e.RoleId = _cte.RoleId 
       and Month(e.EffectiveDate) < _cte.Month and Year(e.EffectiveDate) < _cte.Year 
       order by EffectiveDate desc 
      ) sub 
    where _cte.EffectiveDate is null 

例子:

enter image description here

enter image description here

enter image description here

我正在努力编写与此一致的查询。起初,我试图按最大日期进行分组。但是,当我执行加入时,我获得了每个单一时期(甚至是生效日期之前的最高生效日期)的最高生效日期。

这是可以在查询中完成的事情,还是我应该专注于目标表的增量更新,以便过去的任何有效日期/时间段都是独立的?

任何提示将是伟大的!

感谢, 钱宁

+1

我们需要看到参与(最好的测试数据)表和参与SCD列的DDL。 – NickyvV

+0

在SCD中调用的列是ConsultantId,ProjectId和RateId。不断变化的领域是RateId。一旦新的费率被分配一个新的EngagementId被形成。然后,根据月份和年份,我们将这个新的作品与其ConsultantId和ProjectId对应的作品进行匹配。生效日期是在创建新费率时形成的。对于超过该生效日期的每个月一年,应该与新的顾问,项目和费率匹配。我们是否应该忽视Engagment,因为它更关心顾问,项目和评分?创建顾问,项目和费率的合约关键? – Channing

回答

0

试试这个:

; with _CTE as(
    select p.* , c.EffectiveDate, c.Cost 
    from period p 
     left join CostDimension c on p.id = c.id and p.Month = DATEPART(month, c.EffectiveDate) and p.year = DATEPART (year, EffectiveDate) 
) 
select * from _CTE Where EffectiveDate is not null 

Union 

select _CTE.id, _CTE.Month, _CTE.Year, sub.EffectiveDate, sub.Cost 
from _CTE 
    outer apply (select top 1 EffectiveDate, Cost 
        from CostDimension as cd 
        where cd.Id = _CTE.id and cd.EffectiveDate < DATETIMEFROMPARTS(_CTE.Year, _CTE.Month, 1, 0, 0, 0, 0) 
        order by EffectiveDate desc 
       ) sub 
where _Cte.EffectiveDate is null 
+0

我对该帖子进行了修改以澄清。我将加入两个单独的表格。我希望加入时考虑到更改/日期和成本。对不起,如果我太模糊了! – Channing

+0

所以,只是为了澄清...'成本 - 期间事实'是你的预期产出,另外两个表是输入。对? – Sparrow

+0

是的,我将加入期间和成本。每增加一个新的生效日期后,我都希望从那一刻开始的所有时间段都能反映出适当的成本,同时还要保留任何期间/更改之前的成本。 – Channing