码迷,mamicode.com
首页 > 数据库 > 详细

sql server 中判断分组排序的使用示例

时间:2017-02-03 19:54:53      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:blog   sql   范围   highlight   使用   排序   查询   count   tab   

现在需要查询一组数据,是对一列字段(column01)的数据分范围查询后分组排序:

select (case
when [column01] >0 AND [column01]<= 500 THEN 1
when [column01] >500 AND [column01]<= 1000 THEN 2
when [column01] >1000 AND [column01]<= 5000 THEN 3
when [column01] >5000 AND [column01]<= 20000 THEN 4
when [column01] >20000 AND [column01]<= 40000 THEN 5
else 0
end) f,
count(1) cnt from your_table WHERE [column01] > 0 group by 
(case
when [column01] >0 AND [column01]<= 500 THEN 1
when [column01] >500 AND [column01]<= 1000 THEN 2
when [column01] >1000 AND [column01]<= 5000 THEN 3
when [column01] >5000 AND [column01]<= 20000 THEN 4
when [column01] >20000 AND [column01]<= 40000 THEN 5
else 0
end)

  

sql server 中判断分组排序的使用示例

标签:blog   sql   范围   highlight   使用   排序   查询   count   tab   

原文地址:http://www.cnblogs.com/briswhite/p/6363188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!