首页 技术 正文
技术 2022年11月14日
0 收藏 669 点赞 3,730 浏览 883 个字

1、有如下数据,要求查询每个班最低分和最高分,并将最高分与最低分显示为同一列

ID Student CourseName Score
1 张三 English 80
2 张三 Math 70
3 张三 Chinese 50
4 李四 English 90
5 李四 Chinese 70
6 王五 Math 60
7 王五 English 70
8 赵六 Chinese 80
9 赵六 Math 60
10 赵六 English 90

这是我的解决方案,如有更好的,欢迎指点:

select Student,CourseName,Score
from(
  select Student,CourseName,Score,
  rnDesc=ROW_NUMBER() over(partition by CourseName order by Score desc),–按照分数降序给个编号(如果是按照学生的话将over()中的CourseName改为

–Student 即可)
  rnAsc=ROW_NUMBER() over(partition by CourseName order by Score Asc)–按照分数升序给个编号
  from dbo.Score

)T where T.rnAsc=1 or t.rnDesc=1–取按升序的第一个和按降序的第一个即为最低分和最高分

2、如题:

ID    Numb  type

10001in
20001in
30001out
50001in
60002in
70002out
80002in
90002in
100003out
110003out
120004in

要求查出的结果格式为:

numb    in     out
0001 3 1
0002 3 1
0003 0 2
0004 1 0

sql:

select numb,

sum(case type when ‘in’ then 1 else 0 end)as tIn,–统计type为”in”的数量,用sum而非count

sum(case type when ‘out’ then 1 else 0 end)as tOut,–统计type为”out”的数量,用sum而非count

from table1

group by numb

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900