使用日期字段匹配SQL查询

时间:2016-09-13 20:42:39

标签: sql sql-server datetime scd

我有一个问题围绕着这个不断变化的维度的逻辑。我想将下面的这两个表联系起来。我需要根据Id和生效日期将Cost - Period事实表与成本维度相匹配。

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

编辑:我为缺乏细节而道歉,但成本维度实际上会有一个唯一的索引值,而匹配的更改字段将是资源,项目,成本。我试图将您提供的查询与我的字段匹配,但我得到的输出不正确。

仅供参考:命名约定更改:EngagementId为Id,Resource为ConsultantId,Project为ProjectId

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

     ,_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 个答案:

答案 0 :(得分: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