sql 分组查询问题
发布时间:2024-01-21 05:50:00
情景一:
表中数据
name score
aaa 11
aaa 19
bbb 12
bbb 18
ccc 19
ddd 21
期望查询结果如下
name score
aaa 30
bbb 30
ccc 19
ddd 21
---检查表是否存在
if exists(select * from sysobjects where name='testSum')
drop table testSum
go
---创建表
create table testSum
(
tid int primary key identity(1,1),
tname varchar(30) null,
tscor int null
)
go
insert into testSum (tname,tscor)
select 'aaa',11
union all
select 'aaa',19
union all
select 'bbb',12
union all
select 'bbb',18
union all
select 'ccc',19
union all
select 'ddd',21
---查询语句
select tname ,sum(tscor) from testSum group by tname
---只查询tscor总和为30的
select tname ,sum(tscor) from testSum group by tname having sum(tscor)=30
情景二:
姓名 科目 分数
张三 语文 30
张三 数学 50
张三 英语 70
李四 语文 50
李四 数学 80
李四 英语 90
期望查询结果:
姓名 语文 数学 英语
张三 30 50 70
李四 50 80 90
---检查表是否存在
if exists(select * from sysobjects where name='testScore')
drop table testScore
go
---创建表
create table testScore
(
tid int primary key identity(1,1),
tname varchar(30) null,
ttype varchar(10) null,
tscor int null
)
go
---插入数据
insert into testScore values ('张三','语文',90)
insert into testScore values ('张三','数学',20)
insert into testScore values ('张三','英语',50)
insert into testScore values ('李四','语文',30)
insert into testScore values ('李四','数学',47)
insert into testScore values ('李四','英语',78)
---查询
select tname as '姓名' ,
max(case ttype when '语文' then tscor else 0 end) '语文',
max(case ttype when '数学' then tscor else 0 end) '数学',
max(case ttype when '英语' then tscor else 0 end) '英语'
from testScore
group by tname
情景三:
表:table1
字段:id , name
内容:
----------------
1,aaa
1,bbb
2,ccc
2,ddd
3,eee
3,fff
--------------
希望结果:
---------------------
1 aaa bbb [aaa bbb之间半角空格区分,以下类似]
2 ccc ddd
3 eee fff
f exists(select * from sysobjects where name='test1')
drop table test1
go
create table test1
(
tid int primary key identity(1,1),
tnum int null,
tname varchar(30) null
)
go
insert into test1 values (1,'aa')
insert into test1 values (1,'bb')
insert into test1 values (2,'cc')
insert into test1 values (2,'dd')
insert into test1 values (3,'ee')
insert into test1 values (3,'ff')
SELECT * FROM ( SELECT DISTINCT tnum FROM test1
)A
OUTER APPLY(
SELECT tname= STUFF(REPLACE(REPLACE(
(
SELECT tname FROM test1 N
WHERE tnum = A.tnum
FOR XML AUTO
), '<N tname="', ' '), '"/>', ''), 1, 1, '')
)N
情景四:
我需要将表tb中的数据select出来,得到下面第二个表的数据,如何写select语句?
表tb
id a flag class
----------+---------+--------+---------
1 2 1 A
2 2 1 A
3 4 1 A
4 5 2 A
5 3 2 A
6 4 1 A
7 2 1 A
8 3 2 A
9 4 2 A
10 5 3 A
11 5 1 B
12 2 1 B
13 3 1 B
14 4 1 B
15 2 3 B
16 7 3 B
17 3 2 B
18 4 1 B
19 5 1 B
20 2 2 B
21 1 1 B
22 1 1 C
23 2 3 C
24 6 3 C
25 3 2 C
...
需要选取出如下的表,按class列进行分组,a1,a2,a3字段分别为flag=1、2、3时tb表中a字段的求和
选取后
a1 a2 a3 class
-----------+------------+-----------------+--------------
sum(a) sum(a) sum(a) A
sum(a) sum(a) sum(a) B
sum(a) sum(a) sum(a) C
sum(a) sum(a) sum(a) D
sum(a) sum(a) sum(a) E
sum(a) sum(a) sum(a) F
sum(a) sum(a) sum(a) G
---检查表是否存在
if exists(select * from sysobjects where name='testFlag')
drop table testFlag
go
---创建表
create table testFlag
(
tid int primary key identity(1,1),
tname varchar(30) null,
tflag int null,
tscor int null
)
go
---插入数据
insert into testFlag (tname,tflag,tscor)
select 'aaa',1,11
union all
select 'aaa',2,19
union all
select 'aaa',3,12
union all
select 'aaa',1,18
union all
select 'aaa',2,19
union all
select 'aaa',3,21
union all
select 'bbb',1,11
union all
select 'bbb',2,19
union all
select 'bbb',3,12
union all
select 'bbb',1,18
union all
select 'bbb',2,19
union all
select 'bbb',3,21
----查询语句
select distinct tname,(select sum(tscor) from testFlag where tflag=1 and testFlag.tname = t.tname) as 'flag1',(select sum(tscor) from testFlag where tflag=2 and testFlag.tname = t.tname) as 'flag2',(select sum(tscor) from testFlag where tflag=3 and testFlag.tname = t.tname) as 'flag3' from testFlag t group by tname,tflag
猜你喜欢
- yolov5的代码模型构建是通过.yaml文件实现的,初次看上去会一头雾水,这里记录一下,也方便自己后面用到的时候查看。以models/yo
- 具体方法:1使用panda read_excel 方法加载excel2使用concat将DataFrame列表进行拼接3然后使用pd.Exc
- 前言:学过C语言肯定接触过排序问题,我们最常用的也就是冒泡排序、选择排序、插入排序……等等,同样
- Ajax的流行给用户体验带来了很大程序的提升,而“注册“这项做为互联网最常用到的功能也自然而然的成为Ajax最常光顾的地方,实时判断用户输入
- 前言:过完520,咱们来玩玩五子棋陶冶情操。快拿这个和你女朋友去对线。多的不说直接进入正题人人对战游戏规则:p1为黑子,p2为白子,黑子先手
- 自从2005年8月11日阿里巴巴宣布收购雅虎中国的全部资产后,做为阿里巴巴集团的创始人马云心里盘算的事应该是如何把雅虎中虎整合进阿里巴巴这个
- 1.安装step1step 2 首先是更改安装地址,推荐安装在D盘,如果和我一样C盘容量大的话,也可以不改,直接点击【Next】😄step3
- pydev debugger: process 10341 is connecting无法debu今天在Pycharm中debug时无法正常
- 前言上机实践课程开始了,嗯,老师来了之后念了下PPT,然后说:开始做吧.........然后就开始了Python的GUI之路,以前没接触过P
- 从 PDF 表格中获取数据是一项痛苦的工作。不久前,一位开发者提供了一个名为 Camelot 的工具,使用三行代码就能从 PDF 文件中提取
- 首先介紹一下我們用360搜索派取城市排名前20。我们爬取的网址:https://baike.so.com/doc/24368318-2518
- python中自定义模块导入路径的方式主要有以下3种:(1)使用sys.path.append()随着程序执行,会动态地添加模块导入的路径,
- 笔者remove TensorFlow总共四次。 reinstall anaconda 三次。安装技巧可以根据这个博主的文章进行安装。htt
- python中没有swich..case,若要实现一样的功能,又不想用if..elif来实现,可以充分利用字典进行实现主要是想要通过不同的k
- 如下所示:from mpl_toolkits.mplot3d import axes3dimport matplotlib.pyplot a
- 需求问题在日常工作中,对于前端发送过来的请求,后端django大部分都是采用json格式返回,也有采用模板返回视图的方式。在模板返回视图的方
- 使用pandas读xlsx文件读取前n行数据读取指定数据(指定行指定列)获取文件行号和列标题将数据转换为字典形式import pandas
- YUI Compressor 压缩 JavaScript 的内容包括:移除注释移除额外的空格细微优化标识符替换(Identifier Rep
- set 是一个无序的元素集合,支持并、交、差及对称差等数学运算, 但由于 set 不记录元素位置,因此不支持索引、分片等类序列的操作。初始化
- #!/usr/bin/env python#coding=utf-8import osfrom pyinotify import Watch