account createddate closed_date account_type Debit_Amount txn_date
1234 01/02/2023 01/01/2099 Normal 100 01/02/2023
7892 02/02/2023 01/01/2099 Premimum 200 01/02/2023
4567 03/02/2023 01/01/2099 Normal 500 01/02/2023
8790 05/02/2023 01/01/2099 Normal 500 05/02/2023
8890 05/02/2023 01/01/2099 Super_prem 500 05/03/2023
8330 06/02/2023 01/01/2099 Normal 500 05/02/2023
8990 08/02/2023 01/01/2099 Normal 500 04/02/2023
8490 04/02/2023 01/01/2099 Premimum 500 05/03/2023
8550 05/02/2023 01/01/2099 Normal 500 05/03/2023
8660 05/02/2023 01/01/2099 Super_prem 500 05/03/2023
8340 06/02/2023 01/01/2099 Normal 500 05/02/2023
8120 08/02/2023 01/01/2099 Normal 500 02/02/2023
8890 04/02/2023 01/01/2099 Premimum 500 05/03/2023
I have some data like this, now I need to report the total debit balance based on account_type But each account type has a different business month cycle to consider.
For example:
For Normal account type I need to consider date from every month 2nd to next month 2nd as business month first business month: for example 2nd January to 2nd Feb is my January business month.
Similarly for Premimum 5 to 5th and Super_premimum 10th to 10th.
I need to report business month wise for last 6 months total debit balance for all these 3 account type.
I was trying to usse DATEADD for each month and account_type and aggregate the debit balance separately and join them later
select convert(date, dateadd(day, 2 - day(getdate()), getdate())) as month_first,
dateadd(day, 2, eomonth(getdate(), -1)) as alternative_month_first,
eomonth(getdate())+2 as month_last
and query for each month in the above for each account type separately but I feel this gets complex and not optimal, is there a better way to do it ?