У меня очень длинная инструкция CASE, которая просто изменяет числа в 3 разных местах CASE. Можно ли создать какую-то форму цикла и уменьшить этот код?
Это продолжается в течение 27 итераций, поэтому я сократил его, чтобы показать только первые 5. Любая помощь приветствуется!
SELECT [Loan Key]
,[Trans_Code]
,[Date]
,[NSF Charge]
,[Date Paid]
,[Amount Paid]
,CASE WHEN LEAD(TRANS_CODE, 1) OVER(PARTITION BY [Loan Key] ORDER BY [Date]) = '36' THEN [Amount Outstanding]
WHEN LEAD([Loan Key], 2) OVER(PARTITION BY [Loan Key] ORDER BY [Date]) = LEAD([Loan Key], 1) OVER(PARTITION BY [Loan Key] ORDER BY [Date])
AND LEAD(TRANS_CODE, 2) OVER(PARTITION BY [Loan Key] ORDER BY [Date]) = '36' THEN LEAD([Amount Outstanding], 1) OVER(PARTITION BY [Loan Key] ORDER BY [Date])
WHEN LEAD([Loan Key], 3) OVER(PARTITION BY [Loan Key] ORDER BY [Date]) = LEAD([Loan Key], 2) OVER(PARTITION BY [Loan Key] ORDER BY [Date])
AND LEAD(TRANS_CODE, 3) OVER(PARTITION BY [Loan Key] ORDER BY [Date]) = '36' THEN LEAD([Amount Outstanding], 2) OVER(PARTITION BY [Loan Key] ORDER BY [Date])
WHEN LEAD([Loan Key], 4) OVER(PARTITION BY [Loan Key] ORDER BY [Date]) = LEAD([Loan Key], 1) OVER(PARTITION BY [Loan Key] ORDER BY [Date])
AND LEAD(TRANS_CODE, 4) OVER(PARTITION BY [Loan Key] ORDER BY [Date]) = '36' THEN LEAD([Amount Outstanding], 3) OVER(PARTITION BY [Loan Key] ORDER BY [Date])
ELSE 0
END AS [Amount Outstanding2]
,[Amount Waived]
INTO #Summary4
FROM #Summary3
WHERE [Loan Key] = '94523010'