I have a current stored procedure.
It pulls data from the current month based on the criteria. I want to add some columns that will allow me to compare data from last month, and months back. How can I add that. I realize that the same account could be pulled from the previous month, so that may factor in the thought process too.
Looking for an output like this:
Loan Number
Maturity Date
Note Date
Current Balance
Company Number
Imported Date
Deferred Term
MarchNetIncomeAccretion **New Column
MarchNetCostAccretion **New Column
NetIncomeAccretion
NetCostAccretion
Code:
SELECT
CASE WHEN datediff(month, LBAL.date_contract_purchased, LBAL.note_date) > 0 THEN
CASE WHEN LBAL.note_date < LBAL.imported_date THEN 0
ELSE
CASE WHEN LBAL.current_balance > 0 THEN
CASE WHEN datediff(month, LBAL.date_contract_purchased, LBAL.note_date) > 1 THEN
((LH.amount_financed * (LH.Dealer_Fee/100)) + LH.Non_Filing_Fees +
(LH.amount_financed * (LH.Manufacturer_Fee/100)))/(datediff(month, LBAL.date_contract_purchased, LBAL.note_date))
ELSE
((LH.amount_financed * (LH.Dealer_Fee/100 )) + LH.Non_Filing_Fees +
(LH.amount_financed * (LH.Manufacturer_Fee/100))) / (LBAL.number_of_payments)
END
END
END
ELSE
CASE WHEN LBAL.current_balance > 0 THEN
CASE WHEN datediff(month, LBAL.date_contract_purchased, LBAL.note_date) > 1 THEN
((LH.amount_financed * (LH.Dealer_Fee/100)) + LH.Non_Filing_Fees +
(LH.amount_financed * (LH.Manufacturer_Fee/100)))/(datediff(month, LBAL.date_contract_purchased, LBAL.note_date))
ELSE
((LH.amount_financed * (LH.Dealer_Fee/100 )) + LH.Non_Filing_Fees +
(LH.amount_financed * (LH.Manufacturer_Fee/100))) / (LBAL.number_of_payments)
END
END
END
AS NetIncomeAccretion,
CASE WHEN datediff(month, LBAL.date_contract_purchased, LBAL.note_date) > 0 THEN
CASE WHEN LBAL.note_date < LBAL.imported_date THEN 0
ELSE
CASE WHEN LBAL.current_balance > 0 THEN
CASE WHEN datediff(month, LBAL.date_contract_purchased, LBAL.note_date) > 1 THEN
Case When non_filing_fees > 50 then
0 - ((7.17 + (non_filing_fees - 50))/ (datediff(month, LBAL.date_contract_purchased, LBAL.note_date)) )
ELSE
0 - (7.17/ (datediff(month, LBAL.date_contract_purchased, LBAL.note_date)) )
END
END
ELSE
0 - ( 7.17/ (LBAL.number_of_payments) )
END
END
ELSE
CASE WHEN LBAL.current_balance > 0 THEN
CASE WHEN datediff(month, LBAL.date_contract_purchased, LBAL.note_date) > 1 THEN
Case When non_filing_fees > 50 then
0 - ((7.17 + (non_filing_fees - 50))/ (datediff(month, LBAL.date_contract_purchased, LBAL.note_date)) )
ELSE
0 - (7.17/ (datediff(month, LBAL.date_contract_purchased, LBAL.note_date)) )
END
ELSE
0 - (7.17/ (LBAL.number_of_payments) )
END
END
END
AS NetCostAccretion,
(LH.amount_financed * (LH.Dealer_Fee/100)) + LH.Non_Filing_Fees +
(LH.amount_financed * (LH.Manufacturer_Fee/100)) as Total_Fees
FROM
monthly_loan_ledger_balance LBAL
join Avalanche..Loan_Header LH
on LBAL.Loan_Number = LH.Loan_Number
where ((day(LBAL.imported_date) > 25
AND month(LBAL.imported_date) = '04'
AND year(LBAL.imported_date) = '2007')
OR (day(LBAL.imported_date) < 10
AND month(LBAL.imported_date) = '04' + 01 -- requested month + 1
AND year(LBAL.imported_date) = '2007'))
AND datediff(month, getdate(), LBAL.maturity_date) > 1
AND LBAL.current_balance > 0
AND LBAL.loan_number >= 189625
AND LH.company_number = 03
order by LBAL.current_balance
It pulls data from the current month based on the criteria. I want to add some columns that will allow me to compare data from last month, and months back. How can I add that. I realize that the same account could be pulled from the previous month, so that may factor in the thought process too.
Looking for an output like this:
Loan Number
Maturity Date
Note Date
Current Balance
Company Number
Imported Date
Deferred Term
MarchNetIncomeAccretion **New Column
MarchNetCostAccretion **New Column
NetIncomeAccretion
NetCostAccretion


