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

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

    This will allow you to not have to specify ranges, and should be SQL server agnostic. Math FTW!

    SELECT CONCAT(range,'-',range+9), COUNT(range)
    FROM (
      SELECT 
        score - (score % 10) as range
      FROM scores
    )
    

提交回复
热议问题