Most of our appointments are weekly schedule, but some of them are fortnightly, and some monthly. I have a small script that generates the weekly scheduled day, in this case a Tuesday, weekday 3, using date dimension table.
There is a flag in the appointments table that highlights if the schedule is fortnightly, monthly or weekly, and if it is null it gives weekly schedule date at the moment.
A min date is generated in the code and this is used in the filter to set the date range to look under and generates the weekly dates for scheduled appointments.
I'm trying to figure out a way to set the dates to fortnightly and monthly when the flag highlights it but can't seem to get it to work.
This is a simplified example of the query and the data:
select a.AppReason, a.AssessmentDate, a.PersonID, a.Frequency, a.Weeks, dd.date AS Scheduled_date
from
(select scht.AppReason
, scht.AssesstDate
, scht.PersonID
, scht.Frequency
, scht.Weeks
, MIN(scht.WeekCommence1) min_date
from view_ScheduledTuesday scht
group by scht.AppReason, scht.AssesstDate, scht.PersonID, scht.Frequency, scht.Weeks
) a
cross join dbo.DateDimension dd
where dd.DayOfWeek = 3
and dd.date BETWEEN a.min_date AND GETDATE()
AppReason | AssessmentDate | ClientID | Frequency1 | Weeks1 | Scheduled_date |
---|---|---|---|---|---|
Loan_Review | 2020-02-25 16:43:00.000 | 9876547 | freq_week | 7 | 2020-03-17 |
Cheque_Clr | 2020-02-25 16:43:00.000 | 9876547 | freq_week | 7 | 2020-03-24 |
Savings_Rev | 2020-02-25 16:43:00.000 | 9876547 | freq_week | 7 | 2020-03-31 |
But if the scheduled was fortnightly as below it should be different schedule day
:
AppReason | AssessmentDate | ClientID | Frequency1 | Weeks1 | Scheduled_date |
---|---|---|---|---|---|
Loan_Review | 2020-02-25 16:43:00.000 | 9876547 | freq_fnight | 7 | 2020-03-17 |
Cheque_Clr | 2020-02-25 16:43:00.000 | 9876547 | freq_fnight | 7 | 2020-03-24 |
Savings_Rev | 2020-02-25 16:43:00.000 | 9876547 | freq_fnight | 7 | 2020-03-31 |
First date ok as first date of schedule
Second date in bold Should be 2020-03-31
Third date in bold Should be 2020-04-14
Not entirely sure how to include all of the conditions into the query, as I said it defaults to weekly schedule date, so some sort of alteration to also work with the other schedules is needed.
Thanks
G