博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql server统计字段的值在某些范围内中的个数
阅读量:7121 次
发布时间: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

你可能感兴趣的文章
MySQL server has gone away 问题的解决方法
查看>>
X-NUCA全国高校网安联赛7月训练题解
查看>>
MyEclipse中对项目分类管理
查看>>
mysql 基于 ssl 的主从复制
查看>>
2015.7.29 上学前在家的最后一晚
查看>>
Linux自学笔记——iptables
查看>>
给力的网络 有道的性能——802.11n与WLAN
查看>>
NO.59 禅道的获奖奖品
查看>>
Git前世今生-版本控制软件的发展
查看>>
asa802.k8-telnet for lan-base
查看>>
将勾选数据从dataset中筛选出来
查看>>
SylixOS启动读取配置文件
查看>>
Inspex
查看>>
volatile关键字使用总结
查看>>
Apache 别名与重定向
查看>>
用户和组练习题
查看>>
ios启动私有链查询区块信息
查看>>
gcc下strstream使用时报错
查看>>
jsp 连接sql数据库查询(源代码)
查看>>
Vue.js视频教程
查看>>