'Select Statement inside of
I get error "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression." When I set @MonthYear = 2. = to 1 it work just as I want it. What can I change here?
insert into #LoansTable (Year ,LoanType,ProcessDate ,Month ,Balance ,RowNum, PercentChange, LastCol
)
select
x.YEAR,LoanType,ProcessDate,Month,Balance,RowNum
--,Cast(SUM( Balance - LastCol )/LastCol * 100 as nvarchar(20)) +' ' +'%' as PercentChange
,case when LastCol = 0 then 0
else SUM( Balance - LastCol )/LastCol * 100
end as PercentChange
, LastCol
from
(
SELECT *,
case when @MonthYear = 1 then isnull(LEAD(Balance,1) over (order by rownum),2)
When @MonthYear = 2 then (select balance from #MonthlyLoanBalance2 where ProcessDate = convert(Varchar(8),DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0))),112))
end as LastCol
FROM #MonthlyLoanBalance2
)x
Group by x.YEAR,ProcessDate,Month,Balance,RowNum,Loantype,LastCol
order by year desc, RowNum
Select * from #MonthlyLoanBalance2
My code I want to pull the last day of the previous year based on toggle that I setup in another reporting system. If 1 it will return the prevous Month end of month balance. If 2 then it will return previous years end of month balance. (December 31st) Previous month works as desired. Just need the previous years end balance so my other calculations work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|