I have the below query which is taking more than 40 mins to execute. I am new to sql and not sure if it can be optimized.
The query is calculating Interest, Principal and Payable for each month starting from Oct – Sept using the Vend_Cal tbl. All the calculated fields along with the fields from the table are required as output.
Nov calculations are dependent on Oct, Dec are dependent on Nov and so on until Sept.
I am using a cte for each month and pushing down those calculations until Sept. Is there a better way to accomplish this? Thank you.
Table: Vendor_Calc
Unique Id | Check | Rate | AT_Lb | Oct_Vend | Nov_Vend | Dec_Vend | Jan_Vend | Feb_Vend | Mar_Vend | Apr_Vend | May_Vend | Jun_Vend | Jul_Vend | Aug_Vend | Sept_Vend |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
3301 | A | 15.26 | 5560.10 | 5431 | 1009 | 560 | 3500 | 3800 | 5000 | 1250 | 2000 | 5600 | 3450 | 1250 | 3000 |
3302 | I | 20.98 | 4100.90 | 6700 | 8700 | 3456 | 4600 | 9000 | 1250 | 1450 | 3000 | 3450 | 4550 | 3550 | 4000 |
3303 | I | 19.75 | 7213.00 | 8901 | 7689 | 1000 | 7000 | 7000 | 4560 | 6000 | 8000 | 9000 | 2540 | 6550 | 7000 |
3304 | A | 55.02 | 550.90 | 2500 | 3500 | 1200 | 8900 | 450 | 6000 | 4000 | 6500 | 6000 | 6750 | 7500 | 5000 |
3305 | I | 32.98 | 375.21 | 1546 | 4321 | 3500 | 9510 | 550 | 7600 | 2000 | 3500 | 3000 | 9500 | 8900 | 8000 |
3306 | I | 12.80 | 1455.00 | 2000 | 1500 | 7600 | 3500 | 600 | 8000 | 1750 | 1290 | 2000 | 3590 | 9000 | 2500 |
3307 | I | 13.00 | 5210.10 | 7000 | 2450 | 8500 | 5000 | 900 | 4500 | 8500 | 2400 | 1450 | 2500 | 1250 | 1200 |
3308 | I | 24.95 | 300.45 | 8000 | 8070 | 4500 | 1000 | 250 | 7800 | 6000 | 5490 | 4550 | 1500 | 3500 | 7000 |
This is the query I have:
with cte as
(select * from Vendor_Calc),
Oct as (
select *,
0 as Oct_Interest,
case when Check ='I' then Oct_Vend- 0 else 0 end Oct_Principal,
round(AT_lb - case when Check ='I' then (Oct_Vend- 0 else 0 end, 2) as Oct_Payable
from cte
),
Nov as (
select a. *,
round(case when Check ='I' then Nov_Vend - Nov_Interest else 0 end,2) as Nov_Principal,
round(AT-Lb - case when Check ='I' then Nov_Vend -Nov_Interest else 0 end,2) as Nov_Payable
from
(
select *,
round(case when Nov_Vend>0 then Oct_Payable * Rate else 0 end,2) as Nov_Interest
from Oct
)a
),
Dec as (
select a. *,
round(case when Check ='I' then Dec_Vend - Dec_Interest else 0 end,2) as Dec_Principal,
round(AT-Lb - case when Check ='I' then Dec_Vend -Dec_Interest else 0 end,2) as Dec_Payable
from
(
select *,
round(case when Dec_Vend>0 then Nov_Payable * Rate else 0 end,2) as Dec_Interest
from Nov
)a
),
Jan as (
select a. *,
round(case when Check ='I' then Jan_Vend - Jan_Interest else 0 end,2) as Jan_Principal,
round(AT-Lb - case when Check ='I' then Jan_Vend -Jan_Interest else 0 end,2) as Jan_Payable
from
(
select *,
round(case when Jan_Vend>0 then Dec_Payable * Rate else 0 end,2) as Jan_Interest
from Nov
)a
),
Feb as (
select a. *,
round(case when Check ='I' then Feb_Vend - Feb_Interest else 0 end,2) as Feb_Principal,
round(AT-Lb - case when Check ='I' then Feb_Vend -Feb_Interest else 0 end,2) as Feb_Payable
from
(
select *,
round(case when Feb_Vend>0 then Jan_Payable * Rate else 0 end,2) as Feb_Interest
from Jan
)a
),
Mar as (
select a. *,
round(case when Check ='I' then Mar_Vend - Mar_Interest else 0 end,2) as Mar_Principal,
round(AT-Lb - case when Check ='I' then Mar_Vend -Mar_Interest else 0 end,2) as Mar_Payable
from
(
select *,
round(case when Mar_Vend>0 then Feb_Payable * Rate else 0 end,2) as Mar_Interest
from Feb
)a
),
Apr as (
select a. *,
round(case when Check ='I' then Apr_Vend - Apr_Interest else 0 end,2) as Apr_Principal,
round(AT-Lb - case when Check ='I' then Apr_Vend -Apr_Interest else 0 end,2) as Apr_Payable
from
(
select *,
round(case when Apr_Vend>0 then Mar_Payable * Rate else 0 end,2) as Apr_Interest
from Mar
)a
),
May as (
select a. *,
round(case when Check ='I' then May_Vend - May_Interest else 0 end,2) as May_Principal,
round(AT-Lb - case when Check ='I' then May_Vend -May_Interest else 0 end,2) as May_Payable
from
(
select *,
round(case when May_Vend>0 then Apr_Payable * Rate else 0 end,2) as May_Interest
from Apr
)a
),
Jun as (
select a. *,
round(case when Check ='I' then Jun_Vend - Jun_Interest else 0 end,2) as Jun_Principal,
round(AT-Lb - case when Check ='I' then Jun_Vend -Jun_Interest else 0 end,2) as Jun_Payable
from
(
select *,
round(case when Jun_Vend>0 then May_Payable * Rate else 0 end,2) as Jun_Interest
from May
)a
),
Jul as (
select a. *,
round(case when Check ='I' then Jul_Vend - Jul_Interest else 0 end,2) as Jul_Principal,
round(AT-Lb - case when Check ='I' then Jul_Vend -Jul_Interest else 0 end,2) as Jul_Payable
from
(
select *,
round(case when Jul_Vend>0 then Jun_Payable * Rate else 0 end,2) as Jul_Interest
from Jun
)a
),
Aug as (
select a. *,
round(case when Check ='I' then Aug_Vend - Aug_Interest else 0 end,2) as Aug_Principal,
round(AT-Lb - case when Check ='I' then Aug_Vend -Aug_Interest else 0 end,2) as Aug_Payable
from
(
select *,
round(case when Aug_Vend>0 then Jul_Payable * Rate else 0 end,2) as Aug_Interest
from Jul
)a
),
Sep as (
select a. *,
round(case when Check ='I' then Sep_Vend - Sep_Interest else 0 end,2) as Sep_Principal,
round(AT-Lb - case when Check ='I' then Sep_Vend -Sep_Interest else 0 end,2) as Sep_Payable
from
(
select *,
round(case when Sep_Vend>0 then Aug_Payable * Rate else 0 end,2) as Sep_Interest
from Aug
)a
)
Select * from Sept;
I have the query producing the expected results. I tried rewriting using subquery to decrease the execution time but it took longer than the one I have.