I have the table below (its test dates, not all dates are included) and I need to return specific dates based on quarterly and fiscal year periods.
reportdate periodtype
2021-11-12 wk
2021-11-19 wk
2021-11-30 mth
2021-12-03 wk
2021-12-10 wk
2021-12-31 mth
2022-01-07 wk
2022-01-14 wk
2022-01-28 wk
2022-01-31 mth
2022-02-04 wk
I have a condition which I am having difficulty writing in SQL.
For the fiscal year I need to return the all the month end report dates plus the latest weekly reportdate. This I am able to to do using this code:
reportDate between @YTD_Start_Dt and @report_dt and (periodtype = 'mth' or reportDate =@report_dt)
There is a case which occurs the first week of every month (in this case 2022-02-04) where the last monthly report is sometimes not available until the second week. Therefore I need to return all available monthly dates plus the latest weekly plus the last weeks report in the month prior.
So for 2022-02-04 I would need to return:
--if current day is less than 8 then return:
2021-11-30
2021-12-31
2022-01-28
2022-02-04
--if current day is greater or equal to 8 then return
2021-11-30
2021-12-31
2022-01-31
2022-02-04
I don't know how to write the logic so that it returns the correct combo of reportdates depending on what is available. Any help is appreciated.