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

前端 未结 15 2498
粉色の甜心
粉色の甜心 2020-11-22 15:38

Suppose I have a table with a numeric column (lets call it \"score\").

I\'d like to generate a table of counts, that shows how many times scores appeared in each ran

15条回答
  •  时光说笑
    2020-11-22 16:30

    select t.blah as [score range], count(*) as [number of occurences]
    from (
      select case 
        when score between  0 and  9 then ' 0-9 '
        when score between 10 and 19 then '10-19'
        when score between 20 and 29 then '20-29'
        ...
        else '90-99' end as blah
      from scores) t
    group by t.blah
    

    Make sure you use a word other than 'range' if you are in MySQL, or you will get an error for running the above example.

提交回复
热议问题