SQL Server sql 分组查询问题
2022-11-12 09:53:03
内容摘要
这篇文章主要为大家详细介绍了SQL Server sql 分组查询问题,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!情景一:表中数据name scoreaaa
文章正文
这篇文章主要为大家详细介绍了SQL Server sql 分组查询问题,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
情景一:表中数据name scoreaaa 11aaa 19bbb 12bbb 18ccc 19ddd 21期望查询结果如下name scoreaaa 30bbb 30ccc 19ddd 21代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <code> ---检查表是否存在 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 </code> |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <code> ---检查表是否存在 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 </code> |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <code> 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 </code> |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <code> ---检查表是否存在 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 </code> |
注:关于SQL Server sql 分组查询问题的内容就先介绍到这里,更多相关文章的可以留意
代码注释