Author |
Topic |
rabisco
Starting Member
15 Posts |
Posted - 2013-05-24 : 15:41:09
|
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 ....selectcase when sum(jp.points) between 0 and 1 then '0-1'when sum(jp.points) between 1 and 2 then '1-2'when sum(jp.points) between 2 and 50 then '2-50'when sum(jp.points) between 51 and 100 then '51-100'when sum(jp.points) between 101 and 300 then '101-300'when sum(jp.points) between 301 and 1000 then '301-1000'when sum(jp.points) between 1001 and 1500 then '1001-1500'when sum(jp.points) between 1501 and 2000 then '1501-2000'when sum(jp.points) between 2001 and 3000 then '2001-3000'when sum(jp.points) between 3001 and 4000 then '3001-4000'when sum(jp.points) between 4001 and 5000 then '4001-5000'when sum(jp.points) between 2001 and 3000 then '5001-6000'when sum(jp.points) between 3001 and 4000 then '6001-7000'when sum(jp.points) between 4001 and 5000 then '7001-8000'when sum(jp.points) between 2001 and 3000 then '8001-9000'when sum(jp.points) between 3001 and 4000 then '9001-10000'when sum(jp.points) > 10000 then '10000-above'end as point_range, count(jp.userid) as countofusersfrom StatusLevelPnt jpinner join user ju on jp.userid = ju.useridgroup by jp.userid Returns the following result...Range userid 2-50 32-50 42-50 1 .What i'm looking for is the count of userids who have points in range 2 to 50 etc. |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-05-24 : 15:45:06
|
Like this. SELECT SUM(CASE WHEN jp.points BETWEEN 0 AND 1 THEN 1 ELSE 0 END) AS '0-1', SUM(CASE WHEN jp.points BETWEEN 1 AND 2 THEN 1 ELSE 0 END) AS '1-2', SUM(CASE WHEN jp.points BETWEEN 2 AND 50 THEN 1 ELSE 0 END) AS '2-50'.... |
|
|
rabisco
Starting Member
15 Posts |
Posted - 2013-05-24 : 16:23:16
|
quote: Originally posted by James K Like this. SELECT SUM(CASE WHEN jp.points BETWEEN 0 AND 1 THEN 1 ELSE 0 END) AS '0-1', SUM(CASE WHEN jp.points BETWEEN 1 AND 2 THEN 1 ELSE 0 END) AS '1-2', SUM(CASE WHEN jp.points BETWEEN 2 AND 50 THEN 1 ELSE 0 END) AS '2-50'....
I ran this ....selectsum (case when jp.points between 0 and 1 then 1 else 0 end) as '0-1'end as point_rangefrom StatusLevelPnt jpinner join user ju on userid = ju.useridgroup by jp.userid But I get the error ...Msg 156, Level 15, State 1, Line 4Incorrect syntax near the keyword 'as'. |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-05-24 : 16:25:28
|
You have an extra "end as point_range in there"selectsum (case when jp.points between 0 and 1 then 1 else 0 end) as '0-1'end as point_rangefrom StatusLevelPnt jpinner join user ju on userid = ju.useridgroup by jp.userid |
|
|
rabisco
Starting Member
15 Posts |
Posted - 2013-05-24 : 16:48:06
|
quote: Originally posted by James K You have an extra "end as point_range in there"selectsum (case when jp.points between 0 and 1 then 1 else 0 end) as '0-1'end as point_rangefrom StatusLevelPnt jpinner join user ju on userid = ju.useridgroup by jp.userid
Thanks, but this does not give me what I need. It returns a row for eveery user rather then the total number of users who for whhom the sum of their points is 0 to 1.As a background the following query returns sum of points for every userid....select jp.userid, sum(jp.points) as pointsfrom StatusLevelPnt jpinner join user ju on jp.userid = ju.useridgroup by jp.useridorder by jp.userid i.e. userid sumof points 1234 1 2345 1So what I'm looking for is....range of points number of userdid0 to 1 2 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2013-05-24 : 16:53:57
|
quote: Originally posted by rabisco Thanks, but this does not give me what I need.
Show the full query where you used James' code. His solution should work. If it isn't giving expected results, show sample data and full table DDL. |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-05-24 : 16:54:26
|
Remove the last line that reads "group by jp.userid" |
|
|
rabisco
Starting Member
15 Posts |
Posted - 2013-05-24 : 17:18:45
|
quote: Originally posted by James K Remove the last line that reads "group by jp.userid"
This is the full query...select sum(case when jp.points between 0 and 1 then 1 else 0 end) as '0-1',sum(case when jp.points between 1 and 2 then 2 else 1 end)as '1-2',sum(case when jp.points between 2 and 50 then 50 else 2 end) as '2-50'from StatusLevelPnt jpinner join user ju on jp.userid = ju.userid Now that I have removed the "group by jp.userid', I get this...0-1 1-2 2-50269980 699920 8888226 Which is not right since the total number of userids is 146,000. This the DDL for the StatusLevelPnt tableCREATE TABLE [dbo].[StatusLevelPnt]( [pointID] [bigint] NOT NULL, [userID] [bigint] NOT NULL, [points] [bigint] NOT NULL, ) Sample data 1000 2005 101001 2005 101002 2005 101003 2002 101004 1 101005 1 101006 2002 101007 2002 101008 1 101009 1 101010 1 101011 1 101012 1 101013 1 101014 1 101015 1 101016 1 101017 1 101018 1 101019 1 101020 2003 101021 2002 101022 2002 101023 2005 101024 2005 101025 2005 101026 2005 101027 2005 101028 2005 101029 2005 101030 2005 101031 2005 101032 2005 101033 2005 101034 2005 101035 2005 101036 2005 101037 2005 101038 2005 101039 2005 101040 2005 101041 2005 101042 2005 101043 2005 101044 2005 101045 2005 101046 2005 101047 2005 101048 2005 101049 2005 101050 2005 101051 2005 101052 2005 101053 2005 101054 2005 101055 2005 101056 2005 101057 1 101058 2005 101059 2005 101060 2005 101061 2005 101062 2005 101063 2005 101064 2005 101065 2005 101066 2005 101067 2005 101068 2005 101069 2002 101070 2002 101071 2002 101072 2003 101073 2002 101074 2002 101075 2002 101077 2005 101078 2005 101079 2005 101080 2009 101081 2005 101082 2002 101083 2002 151084 2005 101085 2005 101086 2005 101087 2005 101088 2005 101089 2008 101090 2008 101091 2008 101092 2008 101093 2017 151095 2005 101096 2005 101097 2005 101098 2005 101099 2005 101100 2005 101101 2005 10 |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-05-24 : 19:53:47
|
What is the output you are looking for? Copy and paste the code and run it. It has two queries in there - see if either of those give you what you are looking for.create table #tmp (userid int, points int);insert into #tmp values (1,10),(2,1),(3,1),(4,0),(5,0),(6,1);SELECT SUM(CASE WHEN jp.points >= 0 and jp.points < 1 THEN 1 ELSE 0 END) AS '0-1', SUM(CASE WHEN jp.points >= 1 and jp.points < 2 THEN 1 ELSE 0 END) AS '1-2', SUM(CASE WHEN jp.points >= 2 and jp.points < 50 THEN 1 ELSE 0 END) AS '2-50'from #tmp jp;select * from ( SELECT SUM(CASE WHEN jp.points >= 0 and jp.points < 1 THEN 1 ELSE 0 END) AS '0-1', SUM(CASE WHEN jp.points >= 1 and jp.points < 2 THEN 1 ELSE 0 END) AS '1-2', SUM(CASE WHEN jp.points >= 2 and jp.points < 50 THEN 1 ELSE 0 END) AS '2-50' from #tmp jp) sunpivot (usercount for range in ([0-1],[1-2],[2-50]))Udrop table #tmp; |
|
|
|