| Author |
Topic |
|
Les.61
Starting Member
49 Posts |
Posted - 2010-01-14 : 22:50:25
|
| I need to create a query that will look at the repaymenttype and then based on that data run one of 2 processes. So IF RepaymentType is 'iv' then Select XXXXXX else Select YYYYYI have tried the following use livereportinggoif (select RepaymentType from dbo.Loan_Amortisation ) = 'IV' beginselect PV, InterestRateStandard, RepaymentType,repaymentfrequency,pmt,Repaypermonth, pmtfixed,RepaypermonthFixed,nper,AccountName,-(pv*-1)*(power((1+InterestRateStandard/1200000),nper))-pmt*((power((1+InterestRateStandard/1200000),nper))-1)/(InterestRateStandard/1200000) as EndBalancefrom dbo.Loan_Amortisationendelseselect PV, InterestRateStandard, RepaymentType,repaymentfrequency,pmt,Repaypermonth, pmtfixed,RepaypermonthFixed,nper,AccountName,-(pv*-1)*(power((1+InterestRateStandard/1200000),3))-pmt*((power((1+InterestRateStandard/1200000),3))-1)/(InterestRateStandard/1200000) as EndBalancefrom dbo.Loan_Amortisationand get the error message Msg 512, Level 16, State 1, Line 1Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.Can anyone point me in the right direction? |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-01-14 : 23:06:23
|
Use a CASE expression.select PV, InterestRateStandard, RepaymentType,repaymentfrequency,pmt,Repaypermonth, pmtfixed,RepaypermonthFixed,nper,AccountName,CASE WHEN RepaymentType = 'IV' THEN-(pv*-1)*(power((1+InterestRateStandard/1200000),nper))-pmt*((power((1+InterestRateStandard/1200000),nper))-1)/(InterestRateStandard/1200000) ELSE-(pv*-1)*(power((1+InterestRateStandard/1200000),3))-pmt*((power((1+InterestRateStandard/1200000),3))-1)/(InterestRateStandard/1200000) ENDas EndBalancefrom dbo.Loan_Amortisation |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2010-01-14 : 23:15:17
|
It is complaining about if (select RepaymentType from dbo.Loan_Amortisation ) = 'IV' The select statement will return as many rows as there are in the dbo.Loan_Amortisation table or view. SQL does not know which of those values it should pick and compare to see if it equals 'IV'.I am guessing that for each row in your table, you want to calculate the EndBalance depending on the RepaymentType. In that case, do the test for Repayment type within the select statement as follows:select PV, InterestRateStandard, RepaymentType,repaymentfrequency,pmt,Repaypermonth, pmtfixed,RepaypermonthFixed,nper,AccountName, case when RepaymentType = 'IV' then -(pv*-1)*(power((1+InterestRateStandard/1200000),nper))-pmt*((power((1+InterestRateStandard/1200000),nper))-1)/(InterestRateStandard/1200000) else -(pv*-1)*(power((1+InterestRateStandard/1200000),3))-pmt*((power((1+InterestRateStandard/1200000),3))-1)/(InterestRateStandard/1200000) end as EndBalancefrom dbo.Loan_Amortisation Also, you may want to put explicit decimal points where you want the numbers to be treated as floats as in 1.0 + InterestRateStandard/1200000.0 etc. |
 |
|
|
Les.61
Starting Member
49 Posts |
Posted - 2010-01-14 : 23:17:10
|
| Many thanks. It worked fine. |
 |
|
|
|
|
|