I have a table (PopulationBondsCashSummary) with data structered like this. What I want to do is loop through each record and for each UserID evaluate the UserCount value. If the @userCount is between 2 and 8 then set the @UserPctg value to the UserPctg value pulled from PopulationBondsCashSummary to set my TOP %. The error I am running into is on these two linesSET @UserCount = (SELECT UserCount FROM PopulationBondsCashSummary)SET @USerPctg = (SELECT TOP 5 (CAST(USerPctg * 100 AS Int)) FROM PopulationBondsCashSummary)The error I am getting is "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."USE [QA]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO DECLARE @Level Int SET @Level = 1 DECLARE @numrows Int DECLARE @i int SET @i = 1 SET @numrows = (SELECT COUNT(*) FROM PopulationBondsCashSummary) IF @numrows > 0 WHILE (@i <= (SELECT COUNT(*) FROM PopulationBondsCashSummary)) BEGIN DECLARE @UserCount Int SET @UserCount = (SELECT MAX(UserCount) FROM PopulationBondsCashSummary) --PRINT @UserCount; DECLARE @USerPctg Dec SET @USerPctg = (SELECT MAX((CAST(USerPctg * 100 AS Int))) FROM PopulationBondsCashSummary) --PRINT @UserPctg SELECT UserID, UserCount, USerPctg FROM PopulationBondsCashSummary SELECT TOP (CASE WHEN @Level = 1 AND @UserCount BETWEEN 2 AND 8 THEN @USerPctg WHEN @Level = 1 AND @UserCount BETWEEN 9 AND 15 THEN @USerPctg WHEN @Level = 1 AND @UserCount BETWEEN 16 AND 25 THEN @USerPctg END) PERCENT ActivityDate, AlienFileLocation,BondAmount, BondNumber, UserID FROM PopulationBondsCash_Temp ORDER BY NEWID() SET @i = @i + 1 END
Data Sample:UserID UserCount USerPctgBENOIT III DONALD 1 0.000224ANSON-PRITCHARD BRENDA 352 0.07919KING LORRAINE 512 0.115185DUBOIS RAYMOND 351 0.078965BRIGANTE MATTHEW 456 0.102587ARMELL SHERRY 538 0.121034ST. MARTIN DOMINIC 762 0.171428ROY SCOTT 492 0.110686FRENETTE BERNARD 561 0.126209BRIGGS PEGGY 420 0.094488