Table:
new_table
user_number | diff
2 | 0
1 | 28
2 | 32
1 | 40
1 | 53
1 | 59
1 | 101
1 | 105
2 | 108
2 | 129
2 | 130
1 | 144
|(result)
v
range | number of users
0-20 | 2
21-41 | 3
42-62 | 1
63-83 | 2
84-104 | 1
105-135| 0
136-156| 3
select t.range as [range], count(*) as [number of users]
from (
select case
when diff between 0 and 20 then ' 0-20'
when diff between 21 and 41 then ' 21-41'
when diff between 42 and 62 then ' 42-62'
when diff between 63 and 83 then ' 63-83'
when diff between 84 and 104 then ' 84-104'
when diff between 105 and 135 then ' 105-135'
else '136-156'
end as range
from new_table) t
group by t.diff
Error:
You have an error in your SQL syntax, near '[range], count(*) as [number of users]
from (
select case
when' at line 1
相关问题
- sqlyog export query result as csv
- Groupby with weight
- NOT DISTINCT query in mySQL
- MySQL: conduct a basic search
- Why sometimes there is one of more gap(s) in the v
One obvious mistake : Mysql uses backticks(
), not
[]
(as sqlserver) . Changet.range as [range], count(*) as [number of users]
toHere is general code to group by range since doing a case statement gets pretty cumbersome.
The function 'floor' can be used to find the bottom of the range (not 'round' as Bohemian used), and add the amount (19 in the example below) to find the top of the range. Remember to not overlap the bottom and top of the ranges!
Mysql as a delimiter for keywords uses backtick sign " ` ", not square brackets (like sql server)
You might want to check Are square brackets valid in an SQL query?
I suspect that '[' and ']' are used in Microsoft's SQL but not mysql.
If you have regular ranges, a quicker solution would be to group with the help of div function.
For instance:
Ranges are represented as single digits in that case and you have to know what they mean: 0 = 0-19, 1 = 20-39, 2 = 40-59,...
If you need different ranges use different divider or maybe subtract some number from diff. For instance "(diff - 1) div 10" gives you ranges 1-10, 11-20, 21-30,...
Here's a solution that will work for any magnitude of diff:
Here's some testable code and its output: