0

I'm having a bit of an issue wrapping my head around the logic of this changing dimension. I would like to associate these two tables below. I need to match the Cost - Period fact table to the cost dimension based on the Id and the effective date.

As you can see - if the month and year field is greater than the effective date of its associated Cost dimension, it should adopt that value. Once a new Effective Date is entered into the dimension, it should use that value for any period greater than said date going forward.

EDIT: I apologize for the lack of detail but the Cost Dimension will actually have a unique Index value and the changing fields to reference for the matching would be Resource, Project, Cost. I tried to match the query you provided with my fields, but I'm getting the incorrect output.

FYI: Naming convention change: EngagementId is Id, Resource is ConsultantId, and Project is ProjectId

I've changed the images below and here is my query

     ,_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

Example:

enter image description here

enter image description here

enter image description here

I'm struggling with writing the query that goes along with this. At first I attempted to partition by greatest date. However, when I executed the join I got the highest effective date for every single period (even those prior to the effective date).

Is this something that can be accomplished in a query or should I be focusing on incremental updates of the destination table so that any effective date / time period in the past is left alone?

Any tips would be great!

Thanks, Channing

4

1 回答 1

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
于 2016-09-21T22:19:39.100 回答