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

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

    declare @RangeWidth int
    
    set @RangeWidth = 10
    
    select
       Floor(Score/@RangeWidth) as LowerBound,
       Floor(Score/@RangeWidth)+@RangeWidth as UpperBound,
       Count(*)
    From
       ScoreTable
    group by
       Floor(Score/@RangeWidth)
    

提交回复
热议问题