

SSC Journeyman
Group: General Forum Members
Last Login: Tuesday, October 7, 2014 1:22 PM
Points: 76,
Visits: 235


I have a table which has 2 columns i.e. userid and points. I'm attemtping to get the number of userid in ranges of points i.e. 0 to 1, 2 to 50 etc.
The following query ....
select case when sum(jp.points) between 0 and 1 then '01' when sum(jp.points) between 1 and 2 then '12' when sum(jp.points) between 2 and 50 then '250' when sum(jp.points) between 51 and 100 then '51100' when sum(jp.points) between 101 and 300 then '101300' when sum(jp.points) between 301 and 1000 then '3011000' when sum(jp.points) between 1001 and 1500 then '10011500' when sum(jp.points) between 1501 and 2000 then '15012000' when sum(jp.points) between 2001 and 3000 then '20013000' when sum(jp.points) between 3001 and 4000 then '30014000' when sum(jp.points) between 4001 and 5000 then '40015000' when sum(jp.points) between 2001 and 3000 then '50016000' when sum(jp.points) between 3001 and 4000 then '60017000' when sum(jp.points) between 4001 and 5000 then '70018000' when sum(jp.points) between 2001 and 3000 then '80019000' when sum(jp.points) between 3001 and 4000 then '900110000' when sum(jp.points) > 10000 then '10000above' end as point_range, count(jp.userid) as countofusers from StatusLevelPnt jp inner join user ju on jp.userid = ju.userid group by jp.userid
Returns the following result...
Range userid 250 3 250 4 250 1
What i'm looking for is the count of userids who have points in range 2 to 50 etc. .....
Range userid 250 7




SSCommitted
Group: General Forum Members
Last Login: Yesterday @ 5:38 PM
Points: 1,816,
Visits: 5,914


You are grouping by userid, so of course you get one row per userid....
Try this:
select x.point_range , count(jp.userid) as countofusers from StatusLevelPnt jp inner join user ju on jp.userid = ju.userid cross apply ( SELECT case when sum(jp.points) between 0 and 1 then '01' when sum(jp.points) between 1 and 2 then '12' when sum(jp.points) between 2 and 50 then '250' when sum(jp.points) between 51 and 100 then '51100' when sum(jp.points) between 101 and 300 then '101300' when sum(jp.points) between 301 and 1000 then '3011000' when sum(jp.points) between 1001 and 1500 then '10011500' when sum(jp.points) between 1501 and 2000 then '15012000' when sum(jp.points) between 2001 and 3000 then '20013000' when sum(jp.points) between 3001 and 4000 then '30014000' when sum(jp.points) between 4001 and 5000 then '40015000' when sum(jp.points) between 5001 and 6000 then '50016000' when sum(jp.points) between 6001 and 7000 then '60017000' when sum(jp.points) between 7001 and 8000 then '70018000' when sum(jp.points) between 8001 and 9000 then '80019000' when sum(jp.points) between 9001 and 10000 then '900110000' when sum(jp.points) > 10000 then '10000above' end as point_range ) x group by x.point_range Note: I used a cross apply just to avoid repeating the huge CASE statement in the "group by"
MM
MMGrid AddinMMNose Addin
Forum Etiquette: How to post Reporting Services problemsForum Etiquette: How to post data/code on a forum to get the best help  by Jeff ModenHow to Post Performance Problems  by Gail Shaw




SSC Journeyman
Group: General Forum Members
Last Login: Tuesday, October 7, 2014 1:22 PM
Points: 76,
Visits: 235


mister.magoo (5/24/2013)
You are grouping by userid, so of course you get one row per userid.... Try this: select x.point_range , count(jp.userid) as countofusers from StatusLevelPnt jp inner join user ju on jp.userid = ju.userid cross apply ( SELECT case when sum(jp.points) between 0 and 1 then '01' when sum(jp.points) between 1 and 2 then '12' when sum(jp.points) between 2 and 50 then '250' when sum(jp.points) between 51 and 100 then '51100' when sum(jp.points) between 101 and 300 then '101300' when sum(jp.points) between 301 and 1000 then '3011000' when sum(jp.points) between 1001 and 1500 then '10011500' when sum(jp.points) between 1501 and 2000 then '15012000' when sum(jp.points) between 2001 and 3000 then '20013000' when sum(jp.points) between 3001 and 4000 then '30014000' when sum(jp.points) between 4001 and 5000 then '40015000' when sum(jp.points) between 5001 and 6000 then '50016000' when sum(jp.points) between 6001 and 7000 then '60017000' when sum(jp.points) between 7001 and 8000 then '70018000' when sum(jp.points) between 8001 and 9000 then '80019000' when sum(jp.points) between 9001 and 10000 then '900110000' when sum(jp.points) > 10000 then '10000above' end as point_range ) x group by x.point_range Note: I used a cross apply just to avoid repeating the huge CASE statement in the "group by"
I get this error when I run your query...
Msg 1015, Level 15, State 1, Line 1 An aggregate cannot appear in an ON clause unless it is in a subquery contained in a HAVING clause or select list, and the column being aggregated is an outer reference.




SSCertifiable
Group: General Forum Members
Last Login: Monday, December 15, 2014 2:26 PM
Points: 5,466,
Visits: 7,647


Without sample schema and data we're trying to offer you ideas, not perfect error free code.
If you would like us to be able to provide solutions that are (nearly) guaranteed to work, help us volunteers help you. You'll find what we prefer when we try to give code assistance in the first link in my signature. It includes a #schema for your table, some sample test data, and expected results from the sample data. Otherwise it'll simply be best effort.
 Craig Farrell
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions  Forum Netiquette For index/tuning help, follow these directions. Tally Tables Twitter: @AnyWayDBA




SSCommitted
Group: General Forum Members
Last Login: Yesterday @ 5:38 PM
Points: 1,816,
Visits: 5,914


Sorry, that's the problem with not having sample data to test with...
you probable want something more like this:
select point_range,count(*) from ( select jp.userid , case when sum(jp.points) between 0 and 1 then '01' when sum(jp.points) between 1 and 2 then '12' when sum(jp.points) between 2 and 50 then '250' when sum(jp.points) between 51 and 100 then '51100' when sum(jp.points) between 101 and 300 then '101300' when sum(jp.points) between 301 and 1000 then '3011000' when sum(jp.points) between 1001 and 1500 then '10011500' when sum(jp.points) between 1501 and 2000 then '15012000' when sum(jp.points) between 2001 and 3000 then '20013000' when sum(jp.points) between 3001 and 4000 then '30014000' when sum(jp.points) between 4001 and 5000 then '40015000' when sum(jp.points) between 5001 and 6000 then '50016000' when sum(jp.points) between 6001 and 7000 then '60017000' when sum(jp.points) between 7001 and 8000 then '70018000' when sum(jp.points) between 8001 and 9000 then '80019000' when sum(jp.points) between 9001 and 10000 then '900110000' when sum(jp.points) > 10000 then '10000above' end as point_range from StatusLevelPnt jp inner join user ju on jp.userid = ju.userid group by jp.userid ) as grouped group by point_range
MM
MMGrid AddinMMNose Addin
Forum Etiquette: How to post Reporting Services problemsForum Etiquette: How to post data/code on a forum to get the best help  by Jeff ModenHow to Post Performance Problems  by Gail Shaw




SSC Journeyman
Group: General Forum Members
Last Login: Tuesday, October 7, 2014 1:22 PM
Points: 76,
Visits: 235


Evil Kraig F (5/24/2013) Without sample schema and data we're trying to offer you ideas, not perfect error free code.
If you would like us to be able to provide solutions that are (nearly) guaranteed to work, help us volunteers help you. You'll find what we prefer when we try to give code assistance in the first link in my signature. It includes a #schema for your table, some sample test data, and expected results from the sample data. Otherwise it'll simply be best effort.
Thanks.
Table schema
CREATE TABLE [dbo].[StatusLevelPnt]( [pointID] [bigint] NOT NULL, [userID] [bigint] NOT NULL, [points] [bigint] NOT NULL, )
Sampe data (StatusLevelPnt table)
SELECT'1000','2005','10', UNION ALL SELECT'1001','2005','10', UNION ALL SELECT'1002','2005','10', UNION ALL SELECT'1003','2002','10', UNION ALL SELECT'1004','1','10', UNION ALL SELECT'1005','1','10', UNION ALL SELECT'1006','2002','10', UNION ALL SELECT'1007','2002','10', UNION ALL SELECT'1008','1','10', UNION ALL SELECT'1009','1','10', UNION ALL SELECT'1010','1','10', UNION ALL SELECT'1011','1','10', UNION ALL SELECT'1012','1','10', UNION ALL SELECT'1013','1','10', UNION ALL SELECT'1014','1','10', UNION ALL SELECT'1015','1','10', UNION ALL SELECT'1016','1','10', UNION ALL SELECT'1017','1','10', UNION ALL SELECT'1018','1','10', UNION ALL SELECT'1019','1','10', UNION ALL SELECT'1020','2003','10', UNION ALL SELECT'1021','2002','10', UNION ALL SELECT'1022','2002','10', UNION ALL SELECT'1023','2005','10', UNION ALL SELECT'1024','2005','10', UNION ALL SELECT'1025','2005','10', UNION ALL SELECT'1026','2005','10', UNION ALL SELECT'1027','2005','10', UNION ALL SELECT'1028','2005','10', UNION ALL SELECT'1029','2005','10', UNION ALL SELECT'1030','2005','10', UNION ALL SELECT'1031','2005','10', UNION ALL SELECT'1032','2005','10', UNION ALL SELECT'1033','2005','10', UNION ALL SELECT'1034','2005','10', UNION ALL SELECT'1035','2005','10', UNION ALL SELECT'1036','2005','10', UNION ALL SELECT'1037','2005','10', UNION ALL SELECT'1038','2005','10', UNION ALL SELECT'1039','2005','10', UNION ALL SELECT'1040','2005','10', UNION ALL SELECT'1041','2005','10', UNION ALL SELECT'1042','2005','10', UNION ALL SELECT'1043','2005','10', UNION ALL SELECT'1044','2005','10', UNION ALL SELECT'1045','2005','10', UNION ALL SELECT'1046','2005','10', UNION ALL SELECT'1047','2005','10', UNION ALL SELECT'1048','2005','10', UNION ALL SELECT'1049','2005','10', UNION ALL SELECT'1050','2005','10', UNION ALL SELECT'1051','2005','10', UNION ALL SELECT'1052','2005','10', UNION ALL SELECT'1053','2005','10', UNION ALL SELECT'1054','2005','10', UNION ALL SELECT'1055','2005','10', UNION ALL SELECT'1056','2005','10', UNION ALL SELECT'1057','1','10', UNION ALL SELECT'1058','2005','10', UNION ALL SELECT'1059','2005','10', UNION ALL SELECT'1060','2005','10', UNION ALL SELECT'1061','2005','10', UNION ALL SELECT'1062','2005','10', UNION ALL SELECT'1063','2005','10', UNION ALL SELECT'1064','2005','10', UNION ALL SELECT'1065','2005','10', UNION ALL SELECT'1066','2005','10', UNION ALL SELECT'1067','2005','10', UNION ALL SELECT'1068','2005','10', UNION ALL SELECT'1069','2002','10', UNION ALL SELECT'1070','2002','10', UNION ALL SELECT'1071','2002','10', UNION ALL SELECT'1072','2003','10', UNION ALL SELECT'1073','2002','10', UNION ALL SELECT'1074','2002','10', UNION ALL SELECT'1075','2002','10', UNION ALL SELECT'1077','2005','10', UNION ALL SELECT'1078','2005','10', UNION ALL SELECT'1079','2005','10', UNION ALL SELECT'1080','2009','10', UNION ALL SELECT'1081','2005','10', UNION ALL SELECT'1082','2002','10', UNION ALL SELECT'1083','2002','15', UNION ALL SELECT'1084','2005','10', UNION ALL SELECT'1085','2005','10', UNION ALL SELECT'1086','2005','10', UNION ALL SELECT'1087','2005','10', UNION ALL SELECT'1088','2005','10', UNION ALL SELECT'1089','2008','10', UNION ALL SELECT'1090','2008','10', UNION ALL SELECT'1091','2008','10', UNION ALL SELECT'1092','2008','10', UNION ALL SELECT'1093','2017','15', UNION ALL SELECT'1095','2005','10', UNION ALL SELECT'1096','2005','10', UNION ALL SELECT'1097','2005','10', UNION ALL SELECT'1098','2005','10', UNION ALL SELECT'1099','2005','10', UNION ALL SELECT'1100','2005','10', UNION ALL SELECT'1101','2005','10', UNION ALL
What I'm expecting is
Rangeof points Numberofuserids 01 40 250 20




SSC Journeyman
Group: General Forum Members
Last Login: Tuesday, October 7, 2014 1:22 PM
Points: 76,
Visits: 235


mister.magoo (5/24/2013)
Sorry, that's the problem with not having sample data to test with... you probable want something more like this: select point_range,count(*) from ( select jp.userid , case when sum(jp.points) between 0 and 1 then '01' when sum(jp.points) between 1 and 2 then '12' when sum(jp.points) between 2 and 50 then '250' when sum(jp.points) between 51 and 100 then '51100' when sum(jp.points) between 101 and 300 then '101300' when sum(jp.points) between 301 and 1000 then '3011000' when sum(jp.points) between 1001 and 1500 then '10011500' when sum(jp.points) between 1501 and 2000 then '15012000' when sum(jp.points) between 2001 and 3000 then '20013000' when sum(jp.points) between 3001 and 4000 then '30014000' when sum(jp.points) between 4001 and 5000 then '40015000' when sum(jp.points) between 5001 and 6000 then '50016000' when sum(jp.points) between 6001 and 7000 then '60017000' when sum(jp.points) between 7001 and 8000 then '70018000' when sum(jp.points) between 8001 and 9000 then '80019000' when sum(jp.points) between 9001 and 10000 then '900110000' when sum(jp.points) > 10000 then '10000above' end as point_range from StatusLevelPnt jp inner join user ju on jp.userid = ju.userid group by jp.userid ) as grouped group by point_range
mister.magoo, spot on. Just what I was looking for.
Thanks!



