sql 分组查询问题

所属分类: 数据库 / MsSql 阅读数: 274
收藏 0 赞 0 分享
情景一:
表中数据
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
更多精彩内容其他人还在看

简析SQL Server数据库用视图来处理复杂的数据查询关系

本文我们主要介绍了SQL Server数据库用视图来处理复杂的数据查询关系的相关知识,以及视图的优缺点和创建方式以及注意事项的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server多表查询优化方案集锦

本文我们主要对SQL Server多表查询的优化方案进行了总结,并给出了实际的例子进行性能与效率的对比,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server无日志恢复数据库(2种方法)

SQL Server数据库中的日志文件可能会由于一些突发事件或者失误造成丢失的严重后果,大家都知道,SQL Server数据库中日志文件是很重要的,所以要及时的将丢失的日志文件给找回来。下文就为大家介绍一种恢复数据库日志文件的方法。
收藏 0 赞 0 分享

没有SQL Server数据库时如何打开.MDF文件

本文主要介绍了在安装有Visual Studio 2005或以上的版本的前提下,没有安装SQL Server数据库也可以打开.MDF数据库文件的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

通过格式良好的SQL提高效率和准确性

在本文中,作者将分享如何通过格式良好的SQL语句提升生产率,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

升级SQL Server 2014的四个要点要注意

升级一个关键业务SQL Server实例并不容易,它要求有周全的计划。计划不全会增加遇到升级问题的可能性,从而影响或延迟SQL Server 2014的升级。在规划SQLServer 2014升级时,有一些注意事项有助于避免遇到升级问题,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server 2005数据库还原错误的经典解决方案

本文主要介绍了一个SQL Server 2005数据库还原过程中的错误的解决方案,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server数据库复制失败的原因及解决方法

本文我们主要介绍了SQL Server数据库中由于mssqlserver没有停止造成数据库复制失败的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

10种Java开发者编写SQL语句时常见错误

这篇文章主要介绍了10种Java开发者编写SQL语句时常见错误,当Java开发人员编写SQL语句时,一切都变得不同了。SQL是一种说明式语言,与面向对象思想和命令式思想无关,需要的朋友可以参考下
收藏 0 赞 0 分享

总结SQL执行进展优化方法

谈到优化就必然要涉及索引,就像要讲锁必然要说事务一样,建议读者先了解一下索引。
收藏 0 赞 0 分享
查看更多