Author |
Topic |
evanburen
Posting Yak Master
167 Posts |
Posted - 2011-10-13 : 09:24:29
|
This was working fine until I needed to add an additional CASE @Level statement. I need to run a different select statement based on the level number passed to it. ThanksCREATE PROCEDURE p_CashSample2 @Level Int = NULL, @RecordCount Int = NULL OUTPUTAS SELECT @RecordCount = COUNT(*)FROM PopulationBondsCash_TempCASE @Level WHEN 1 THEN -- *** Level 1 *** SELECT TOP (CASE WHEN @RecordCount BETWEEN 2 AND 8 THEN 2 WHEN @RecordCount BETWEEN 9 AND 15 THEN 2 ELSE 125 END) * FROM PopulationBondsCash_Temp ORDER BY NEWID() WHEN 2 THEN -- *** Level 2 *** SELECT TOP (CASE WHEN @RecordCount BETWEEN 2 AND 8 THEN 2 WHEN @RecordCount BETWEEN 9 AND 15 THEN 3 ELSE 1250 END) * FROM PopulationBondsCash_Temp ORDER BY NEWID() WHEN 3 THEN -- *** Level 3 *** SELECT TOP (CASE WHEN @RecordCount BETWEEN 2 AND 8 THEN 3 WHEN @RecordCount BETWEEN 9 AND 15 THEN 5 ELSE 2000 END) * FROM PopulationBondsCash_Temp ORDER BY NEWID() END |
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2011-10-13 : 10:44:23
|
[code]SET @RecordCount = (SELECT COUNT(*) FROM PopulationBondsCash_Temp);SELECT TOP ( CASE WHEN @Level = 1 AND @RecordCount BETWEEN 2 AND 15 THEN 2 WHEN @Level = 1 THEN 125 WHEN @Level = 2 AND @RecordCount BETWEEN 2 AND 8 THEN 2 WHEN @Level = 2 AND @RecordCount BETWEEN 9 AND 15 THEN 3 WHEN @Level = 2 THEN 1250 WHEN @Level = 3 AND @RecordCount BETWEEN 2 AND 8 THEN 3 WHEN @Level = 3 AND @RecordCount BETWEEN 9 AND 15 THEN 5 ELSE 2000 END ) *FROM PopulationBondsCash_TempORDER BY NEWID();[/code] |
 |
|
evanburen
Posting Yak Master
167 Posts |
Posted - 2011-10-13 : 14:50:49
|
That's much better. ThanksI need to add one more layer of complexity if you don't mind. Instead of basing the TOP value based on the recordcount on the total number of records in the whhole table, I need to base the TOP value based on the number of records per UserID. Something likeSET @RecordCount = (SELECT COUNT(*) FROM PopulationBondsCash_Temp)FROM PopulationBondsCash_TempGROUP BY UserIDSELECT TOP ( CASE --Level 1 WHEN @Level = 1 AND @RecordCount BETWEEN 2 AND 8 THEN 2 WHEN @Level = 1 AND @RecordCount BETWEEN 9 AND 15 THEN 2 WHEN @Level = 1 AND @RecordCount BETWEEN 16 AND 25 THEN 3 --Level 2 WHEN @Level = 2 AND @RecordCount BETWEEN 2 AND 8 THEN 2 WHEN @Level = 2 AND @RecordCount BETWEEN 9 AND 15 THEN 3 WHEN @Level = 2 AND @RecordCount BETWEEN 16 AND 25 THEN 5 --Level 3 WHEN @Level = 3 AND @RecordCount BETWEEN 2 AND 8 THEN 3 WHEN @Level = 3 AND @RecordCount BETWEEN 9 AND 15 THEN 5 WHEN @Level = 3 AND @RecordCount BETWEEN 16 AND 25 THEN 8 END ) --ActivityDate, AlienFileLocation, UserID, BondAmount, BondNumber, COUNT(UserID) OVER (Partition by UserID) AS UserCount FROM PopulationBondsCash_Temp ORDER BY NEWID() |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-14 : 01:48:53
|
[code]SELECT TOP ( CASE--Level 1WHEN @Level = 1 AND Cnt BETWEEN 2 AND 8 THEN 2WHEN @Level = 1 AND Cnt BETWEEN 9 AND 15 THEN 2WHEN @Level = 1 AND Cnt BETWEEN 16 AND 25 THEN 3--Level 2WHEN @Level = 2 AND Cnt BETWEEN 2 AND 8 THEN 2WHEN @Level = 2 AND Cnt BETWEEN 9 AND 15 THEN 3WHEN @Level = 2 AND Cnt BETWEEN 16 AND 25 THEN 5--Level 3WHEN @Level = 3 AND Cnt BETWEEN 2 AND 8 THEN 3WHEN @Level = 3 AND Cnt BETWEEN 9 AND 15 THEN 5WHEN @Level = 3 AND Cnt BETWEEN 16 AND 25 THEN 8END)--ActivityDate, AlienFileLocation, UserID, BondAmount, BondNumber, COUNT(UserID) OVER (Partition by UserID) AS UserCountFROM (SELECT COUNT(1) OVER (PARTITION BY UserID) AS Cnt,* FROMPopulationBondsCash_Temp)t ORDER BY NEWID()[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
evanburen
Posting Yak Master
167 Posts |
Posted - 2011-10-14 : 09:56:26
|
Hi Thanks for this. I'm getting the errorThe reference to column "Cnt" is not allowed in the argument of the TOP clause. Only references to columns at an outer scope or standalone expressions and subqueries are allowed here.ALTER PROCEDURE [dbo].[p_CashSample] @Level Int = NULLAS SELECT TOP ( CASE--Level 1WHEN @Level = 1 AND Cnt BETWEEN 2 AND 8 THEN 2WHEN @Level = 1 AND Cnt BETWEEN 9 AND 15 THEN 2WHEN @Level = 1 AND Cnt BETWEEN 16 AND 25 THEN 3--Level 2WHEN @Level = 2 AND Cnt BETWEEN 2 AND 8 THEN 2WHEN @Level = 2 AND Cnt BETWEEN 9 AND 15 THEN 3WHEN @Level = 2 AND Cnt BETWEEN 16 AND 25 THEN 5--Level 3WHEN @Level = 3 AND Cnt BETWEEN 2 AND 8 THEN 3WHEN @Level = 3 AND Cnt BETWEEN 9 AND 15 THEN 5WHEN @Level = 3 AND Cnt BETWEEN 16 AND 25 THEN 8END)ActivityDate, AlienFileLocation, UserID, BondAmount, BondNumberFROM (SELECT COUNT(1) OVER (PARTITION BY UserID) AS Cnt,* FROMPopulationBondsCash_Temp)t ORDER BY NEWID() |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-14 : 10:39:53
|
[code];With CTEAS(SELECT COUNT(1) OVER (PARTITION BY UserID) AS Cnt,* FROMPopulationBondsCash_Temp)SELECT t.*FROM (SELECT DISTINCT UserId,Cnt FROM CTE)cCROSS APPLY(SELECT TOP ( CASE--Level 1WHEN @Level = 1 AND c.Cnt BETWEEN 2 AND 8 THEN 2WHEN @Level = 1 AND c.Cnt BETWEEN 9 AND 15 THEN 2WHEN @Level = 1 AND c.Cnt BETWEEN 16 AND 25 THEN 3--Level 2WHEN @Level = 2 AND Cnt BETWEEN 2 AND 8 THEN 2WHEN @Level = 2 AND Cnt BETWEEN 9 AND 15 THEN 3WHEN @Level = 2 AND Cnt BETWEEN 16 AND 25 THEN 5--Level 3WHEN @Level = 3 AND c.Cnt BETWEEN 2 AND 8 THEN 3WHEN @Level = 3 AND c.Cnt BETWEEN 9 AND 15 THEN 5WHEN @Level = 3 AND c.Cnt BETWEEN 16 AND 25 THEN 8END) *FROM CTE )tORDER BY NEWID()[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
evanburen
Posting Yak Master
167 Posts |
Posted - 2011-10-14 : 13:23:53
|
Sorry - I must be doing something dumb - Error message: "'CTE' is not a recognized option."ALTER PROCEDURE [dbo].[p_CashSample] @Level Int = NULLWith CTEAS(SELECT COUNT(1) OVER (PARTITION BY UserID) AS Cnt,* FROMPopulationBondsCash_Temp)SELECT t.*FROM (SELECT DISTINCT UserId,Cnt FROM CTE)cCROSS APPLY(SELECT TOP ( CASE--Level 1 WHEN @Level = 1 AND c.Cnt BETWEEN 2 AND 8 THEN 2 WHEN @Level = 1 AND c.Cnt BETWEEN 9 AND 15 THEN 2 WHEN @Level = 1 AND c.Cnt BETWEEN 16 AND 25 THEN 3 END) *FROM CTE )tORDER BY NEWID() |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-14 : 13:35:35
|
are you using sql 2005 and above?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
evanburen
Posting Yak Master
167 Posts |
Posted - 2011-10-14 : 14:20:02
|
Yes, SQL Server 2005 |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-14 : 14:23:15
|
then it should work fine------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
evanburen
Posting Yak Master
167 Posts |
Posted - 2011-10-14 : 14:40:49
|
Sorry - I just needed to do thisALTER PROCEDURE [dbo].[p_CashSample]@Level Int = NULLASWith CTEAS(....Thanks for the help |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-15 : 02:11:35
|
oh...you missed AS...didnt spot that as I was looking into CTE code------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|