sql分组查询问题

情景一:

 

表中数据
name  score
 aaa   11
 aaa   19
 bbb   12
 bbb   18
 ccc   19
 ddd   21

 

期望查询结果如下

name   score
 aaa     30
 bbb     30
 ccc      19
 ddd     21

 

Sql代码 复制代码
  1. ---检查表是否存在   
  2. if exists(select * from sysobjects where name='testSum')   
  3. drop table testSum   
  4. go   
  5. ---创建表   
  6. create table testSum   
  7. (   
  8.    tid int primary key identity(1,1),   
  9.    tname varchar(30) null,   
  10.    tscor int null  
  11. )   
  12. go   
  13. insert into testSum (tname,tscor)    
  14. select 'aaa',11   
  15. union all    
  16. select 'aaa',19   
  17. union all    
  18. select 'bbb',12   
  19. union all  
  20. select 'bbb',18   
  21. union all  
  22. select 'ccc',19   
  23. union all    
  24. select 'ddd',21   
  25. ---查询语句   
  26. select tname ,sum(tscor) from testSum group by tname   
  27.   
  28. ---只查询tscor总和为30的   
  29.  select tname ,sum(tscor) from testSum group by tname having sum(tscor)=30  
---检查表是否存在
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 

 

Sql代码 复制代码
  1. ---检查表是否存在   
  2. if exists(select * from sysobjects where name='testScore')   
  3. drop table testScore   
  4. go   
  5. ---创建表   
  6. create table testScore   
  7. (   
  8.    tid int primary key identity(1,1),   
  9.    tname varchar(30) null,   
  10.    ttype varchar(10) null,   
  11.    tscor int null  
  12. )   
  13. go   
  14. ---插入数据   
  15. insert into testScore values ('张三','语文',90)   
  16. insert into testScore values ('张三','数学',20)   
  17. insert into testScore values ('张三','英语',50)   
  18. insert into testScore values ('李四','语文',30)   
  19. insert into testScore values ('李四','数学',47)   
  20. insert into testScore values ('李四','英语',78)   
  21. ---查询   
  22. select tname as '姓名' ,    
  23. max(case ttype when '语文' then tscor else 0 end'语文',    
  24. max(case ttype when '数学' then tscor else 0 end'数学',    
  25. max(case ttype when '英语' then tscor else 0 end'英语'    
  26. from testScore    
  27. group by tname   
---检查表是否存在
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)
---查询
方法1:
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 
方法2:
select tname as '姓名',
       (select tscor from testScore a where a.ttype='语文' and a.tname =testScore.tname ) as '语文',
       (select tscor from testScore a where a.ttype='数学' and a.tname =testScore.tname ) as '数学',
       (select tscor from testScore a where a.ttype='英语' and a.tname =testScore.tname ) as '英语'
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
-----------------------

Sql代码 复制代码
  1. f exists(select * from sysobjects where name='test1')   
  2. drop table test1   
  3. go   
  4.   
  5. create table test1   
  6. (   
  7.    tid int primary key identity(1,1),   
  8.    tnum int null,   
  9.    tname varchar(30) null  
  10. )   
  11. go   
  12. insert into test1 values (1,'aa')   
  13. insert into test1 values (1,'bb')   
  14. insert into test1 values (2,'cc')   
  15. insert into test1 values (2,'dd')   
  16. insert into test1 values (3,'ee')   
  17. insert into test1 values (3,'ff')   
  18.   
  19. SELECT * FROM ( SELECT DISTINCT tnum FROM test1   
  20. )A   
  21. OUTER APPLY(   
  22. SELECT tname= STUFF(REPLACE(REPLACE(   
  23. (   
  24. SELECT tname FROM test1 N   
  25. WHERE tnum = A.tnum   
  26. FOR XML AUTO   
  27. ), '<N tname="'' '), '"/>'''), 1, 1, '')   
  28. )N  
if 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


 

情景四:

我需要将表1中的数据select出来,得到右面表2的数据,如何写select语句?
                      

方法一.

select a.sName,
max(case a.sXueke when '数学' then a.sumScore else  0 end) as '数学总成绩',
max(case a.sXueke when '语文' then a.sumScore else  0 end) as '语文总成绩',
max(case a.sXueke when '英语' then a.sumScore else  0 end) as '英语总成绩'
from
(
select distinct sName, SUM(sCore) as sumScore, sXueke
from studentInfo1 t group by sName,sXueke)a  group by a.sName


方法二.

select distinct sName,
(select SUM(sCore) from studentInfo1 where sXueke='数学' and studentInfo1.sName=t.sName) as '数学总成绩',
(select SUM(sCore) from studentInfo1 where sXueke='语文' and studentInfo1.sName=t.sName) as '语文总成绩',
(select SUM(sCore) from studentInfo1 where sXueke='英语' and studentInfo1.sName=t.sName) as '英语总成绩'
from studentInfo1 t group by sName,sXueke


 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值