博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql server统计字段的值在某些范围内中的个数
阅读量:7051 次
发布时间:2019-06-28

本文共 1392 字,大约阅读时间需要 4 分钟。

有一张表test如下:

create table test(id int identity(1,1) primary key,num int )

 

插入数据:

insert into test( num) values (1);insert into test( num) values (2);insert into test( num) values (8);insert into test( num) values (15);insert into test( num) values (12);insert into test( num) values (13);insert into test( num) values (14);insert into test( num) values (16);insert into test( num) values (17);insert into test( num) values (5);insert into test( num) values (6);insert into test( num) values (7);insert into test( num) values (16);insert into test( num) values (18);insert into test( num) values (9);insert into test( num) values (10);insert into test( num) values (11);insert into test( num) values (12);insert into test( num) values (19);insert into test( num) values (20);insert into test( num) values (3);insert into test( num) values (4);insert into test( num) values (19);insert into test( num) values (20);insert into test( num) values (17);insert into test( num) values (18);
View Code

 

问题:请用一条sql语句查询 统计出num在 1~6,  10~17,  19~20 这三个范围内的个数分别是多少?

 

解法如下:

select COUNT (case when num between 1 and 6 then 1 end ) as [1-6],COUNT( case when num between 10 and 17 then 2 end ) as [10-17],COUNT (case when num between 19 and 20 then 3 end ) as [19-20]from test

 

如果问题是问1~5, 6~10,11~15这样成倍数(有规律)的话,则可以这样写:

select COUNT(*) from test group by num/5

 

转载于:https://www.cnblogs.com/527289276qq/p/5372172.html

你可能感兴趣的文章
Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
查看>>
sql server的缺陷 将截断字符串或二进制数据 哪个字段
查看>>
把文章里边的html标签去掉(去掉文字的样式,显示css设置的样式)
查看>>
004-线程同步问题引出、同步问题解决、死锁、生产者与消费者
查看>>
学习正则表达式
查看>>
glibc的几个有用的处理二进制位的内置函数(转)
查看>>
vue 跨域:使用vue-cli 配置 proxyTable 实现跨域问题
查看>>
围棋十诀
查看>>
nodejs - 创建服务器(1)
查看>>
Unity读Excel 输出PC端(Windows)后不能读取的问题
查看>>
一台服务器部署多个tomcat
查看>>
shell判断文件是否存在
查看>>
XNA程序开发常用到的一些代码汇总
查看>>
Running ASP.NET Applications in Debian and Ubuntu using XSP and Mono
查看>>
Hudson+Maven+SVN 快速搭建持续集成环境
查看>>
VC++ 编译环境设置 学习之路vs2005
查看>>
Getting Back to a Pure Gnome on Ubuntu
查看>>
Linux之父话糙理不糙 - 51CTO.COM
查看>>
ACM HDU 1404 Digital Deletions(博弈)
查看>>
solution for lost fonts
查看>>