微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在SQL中,如何对范围进行“分组依据”?

如何解决在SQL中,如何对范围进行“分组依据”?

sql Server 2000上,投票率最高的答案均不正确。也许它们使用的是其他版本。

这是它们在sql Server 2000上的正确版本。

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'
    else '20-99' end as range
  from scores) t
group by t.range

或者

select t.range as [score range], count(*) as [number of occurrences]
from (
      select user_id,
         case when score >= 0 and score< 10 then '0-9'
         when score >= 10 and score< 20 then '10-19'
         else '20-99' end as range
     from scores) t
group by t.range

解决方法

假设我有一个带有数字列的表(我们称其为“分数”)。

我想生成一个计数表,该表显示每个范围内得分出现的次数。

例如:

score range  | number of occurrences
-------------------------------------
   0-9       |        11
  10-19      |        14
  20-29      |         3
   ...       |       ...

在此示例中,分数在0到9之间的行有11行,分数在10到19之间的行有14行,分数在20-29范围内的行有3行。

有一个简单的方法来设置它吗?您有什么推荐的吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。