Date split-up based on Fiscal Year

后端 未结 3 1977
南方客
南方客 2021-01-03 07:50

Given a From Date, To Date and a Fiscal Year system, I want to get all the split-up duration within the given From & To Date based on the Fisc

3条回答
  •  青春惊慌失措
    2021-01-03 08:19

    You could create a table containing the start and end of all fiscal years, f.e.

    Periods (PeriodStartDt, PeriodEndDt)
    

    Then you can join the tables together if they at least partly overlap. Use a case statement to select the end of the period or the end of the row, depending on which is later. For example (not tested):

    select      case when yt.StartDt < p.PeriodStartDt then p.PeriodStartDt
                     else yt.StartDt
                end as SplitStart
    ,           case when yt.EndDt > p.PeriodEndDt then p.PeriodEndDt
                     else yt.EndDt
                end as SplitEnd
    ,           yt.*
    from        YourTable yt
    inner join  Periods p
    on          yt.StartDt < p.PeriodEndDate
                and yt.EndDt >= p.PeriodStartDate
    

提交回复
热议问题