数据库教程:sql 分组查询问题

情景一: 表中数据 name score aaa 11 aaa 19 bbb 12 bbb 18 ccc 19 ddd 21 期望查询结果如下 name score aaa

情景一:
表中数据
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

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/619116.html

(0)
上一篇 2021年5月22日
下一篇 2021年5月22日

精彩推荐