准备:

创建一个成绩表

Create table grade (id integer, score integer);

插入数据(只有id每次加一,score是1到100的随机数,java生成):

public class GradeInsertSentence {

public static void main(String[] args) {

for (int i = 0; i < 100; i++) {

int j = (int) (Math.random()*100) + 1;

System.out.println("insert into grade(id,score) value('"+i+"','"+j+"');");

}

}

}

查询grade表的所有数据

Select * from grade;

需求:

查询指定分段的人数(x>=80; 80>x>=60; 60>x>40; 40>x>=20, x<20 )

Sql:

实现1:

select *

from

(select count(*) as A from grade g where g.score >=80) a,

(select count(*) as B from grade g where g.score >=60 and g.score <80) b,

(select count(*) as C from grade g where g.score >=40 and g.score <60) c,

(select count(*) as D from grade g where g.score >=20 and g.score <40) d,

(select count(*) as E from grade g where g.score <20) e;

或者:

select a.aa, b.bb, c.cc, d.dd, e.ee

from

(select count(*) as aa from grade g where g.score >=80) a,

(select count(*) as bb from grade g where g.score >=60 and g.score <80) b,

(select count(*) as cc from grade g where g.score >=40 and g.score <60) c,

(select count(*) as dd from grade g where g.score >=20 and g.score <40) d,

(select count(*) as ee from grade g where g.score <20) e;

实现2:

select count(*) as aa from grade g where g.score >=80

union all

select count(*) as bb from grade g where g.score >=60 and g.score <80

union all

select count(*) as cc from grade g where g.score >=40 and g.score <60

union all

select count(*) as dd from grade g where g.score >=20 and g.score <40

union all

select count(*) as ee from grade g where g.score <20

这个比较尴尬的是显示出来的结果是这样的:

还有就是,如果其中一个分段的是没有值得,那就只会显示4条结果,最重要的是,你还不知道是哪一个分段没有结果。。。。。

实现3:

select

case when (score >=80) then 'A'

when (score >=60 and score <80) then 'B'

when (score >=40 and score <60) then 'C'

when (score >=20 and score <40) then 'D'

else 'E'

end grade, count(*) num

from grade group by

case when (score >=80) then 'A'

when (score >=60 and score <80) then 'B'

when (score >=40 and score <60) then 'C'

when (score >=20 and score <40) then 'D'

else 'E' end

order by 1;

select

case when (score >=80) then 'A'

when (score >=60 and score <80) then 'B'

when (score >=40 and score <60) then 'C'

when (score >=20 and score <40) then 'D'

else 'E'

end 'grade', count(*) num

from grade

group by

case when (score >=80) then 'A'

when (score >=60 and score <80) then 'B'

when (score >=40 and score <60) then 'C'

when (score >=20 and score <40) then 'D'

else 'E' end;

实现4:

select A.score*20, count(A.score) from

(

select floor(g.score/20) as score from grade g

)  A

group by A.score;

或(有错,不会用convert

select convert(A.score*20,varchar) ,count(A.score)   from

(

select floor(g.score/20) as score from grade g

) A

group by A.score;

实现5:(错的)

select

case when score BETWEEN 80 AND 100 then 'A'

when score BETWEEN 60 AND 80 then 'B'

when score BETWEEN 40 AND 60 then 'C'

when score BETWEEN 20 AND 40 then 'D'

when score < 20 then 'E' end as 'grade',

count(*) as 'num' FROM grade;

都是在百度上找的,最后一个实现不成功,between and在select里面不能识别范围,哪位仁兄看到,实现了,记得给我留言,谢谢。

最新文章

  1. IP地址,子网掩码,默认网关,DNS服务器详解
  2. sublime 配置jade高亮显示
  3. 【C#进阶系列】16 数组
  4. 讨论一下hibernate如何动态注册一个动态生成的实体类
  5. JavaScript的学习--正则表达式
  6. Android 监听器
  7. asp.net MVC Razor 语法(3)
  8. ural 1261. Tips(进制运算)
  9. nodejs之socket.io模块——实现了websocket协议
  10. L2-004 这是二叉搜索树吗? (25 分) (树)
  11. 了解AutoCAD对象层次结构 —— 1 ——应用程序
  12. iOS:解决UITextView自适应高度粘贴大量文字导致显示不全的问题
  13. d3生成的树状图
  14. 关于$.ajax同步和异步的问题和提交后台的一些问题。
  15. JS判断手机端是否安装某应用
  16. Duplicate复制数据库并创建物理StandBy(spfile+不同实例名+不同路径)
  17. Docker多主机网络
  18. js 产生随机数
  19. Windows虚拟地址转物理地址(原理+源码实现,附简单小工具)
  20. 17-hadoop-yarn安装

热门文章

  1. JSP内置对象--pageContext对象(非常重要!!!)
  2. nat和打洞
  3. 单片AT89C2051 + SD卡 + 3310LCD = 音乐播放器
  4. PHP的json_encode中文被转码的问题
  5. gen_grant_dml.sql
  6. 二分图匹配之最佳匹配——KM算法
  7. 黄聪:基于Asp.net的CMS系统We7架设实验(环境WIN7,SQL2005,.NET3.5)(初学者参考贴)
  8. (转)StringTokenizer类的使用
  9. CF History(区间合并)
  10. mr本地运行的几种模式