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

前端 未结 15 2190
粉色の甜心
粉色の甜心 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

    I see answers here that won't work in SQL Server's syntax. I would use:

    select t.range 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 range
      from scores) t
    group by t.range
    

    EDIT: see comments

提交回复
热议问题