code_junkie

In SQL, how can you “group by” in ranges?

Nov 11th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. select t.range as [score range], count(*) as [number of occurences]
  2. from (
  3. select case
  4. when score between 0 and 9 then ' 0- 9'
  5. when score between 10 and 19 then '10-19'
  6. else '20-99' end as range
  7. from scores) t
  8. group by t.range
  9.  
  10. select t.range as [score range], count(*) as [number of occurences]
  11. from (
  12. select case
  13. when score between 0 and 9 then ' 0- 9'
  14. when score between 10 and 19 then '10-19'
  15. else '20-99' end as range
  16. from scores) t
  17. group by t.range
  18.  
  19. select t.range as [score range], count(*) as [number of occurences]
  20. from (
  21. select user_id,
  22. case when score >= 0 and score< 10 then '0-9'
  23. when score >= 10 and score< 20 then '10-19'
  24. else '20-99' end as range
  25. from scores) t
  26. group by t.range
  27.  
  28. select t.range as [score range], count(*) as [number of occurences]
  29. from (
  30. select case
  31. when score between 0 and 9 then ' 0-9 '
  32. when score between 10 and 19 then '10-19'
  33. when score between 20 and 29 then '20-29'
  34. ...
  35. else '90-99' end as range
  36. from scores) t
  37. group by t.range
  38.  
  39. create table scores (
  40. user_id int,
  41. score int
  42. )
  43.  
  44. select t.range as [score range], count(*) as [number of occurences]
  45. from (
  46. select user_id,
  47. case when score >= 0 and score < 10 then '0-9'
  48. case when score >= 10 and score < 20 then '10-19'
  49. ...
  50. else '90-99' as range
  51. from scores) t
  52. group by t.range
  53.  
  54. select (score/10)*10 || '-' || (score/10)*10+9 as scorerange, count(*)
  55. from scores
  56. group by score/10
  57. order by 1
  58.  
  59. scorerange | count
  60. ------------+-------
  61. 0-9 | 11
  62. 10-19 | 14
  63. 20-29 | 3
  64. 30-39 | 2
  65.  
  66. LowerLimit UpperLimit Range
  67. 0 9 '0-9'
  68. 10 19 '10-19'
  69. 20 29 '20-29'
  70. 30 39 '30-39'
  71.  
  72. Select
  73. Range as [Score Range],
  74. Count(*) as [Number of Occurences]
  75. from
  76. Ranges r inner join Scores s on s.Score between r.LowerLimit and r.UpperLimit
  77. group by Range
  78.  
  79. SELECT
  80. [score range] = CAST((Score/10)*10 AS VARCHAR) + ' - ' + CAST((Score/10)*10+9 AS VARCHAR),
  81. [number of occurrences] = COUNT(*)
  82. FROM #Scores
  83. GROUP BY Score/10
  84. ORDER BY Score/10
  85.  
  86. select cast(score/10 as varchar) + '-' + cast(score/10+9 as varchar),
  87. count(*)
  88. from scores
  89. group by score/10
  90.  
  91. declare @RangeWidth int
  92.  
  93. set @RangeWidth = 10
  94.  
  95. select
  96. Floor(Score/@RangeWidth) as LowerBound,
  97. Floor(Score/@RangeWidth)+@RangeWidth as UpperBound,
  98. Count(*)
  99. From
  100. ScoreTable
  101. group by
  102. Floor(Score/@RangeWidth)
Add Comment
Please, Sign In to add comment