读书人

MM马上给分,分组语句怎样写?该怎么处

发布时间: 2012-02-29 16:44:10 作者: rapoo

MM马上给分,分组语句怎样写?
我要读取表A中的所有字段,同时得到字段sum(Grade) as GrageSum,按StudentName分组的,就是说相等StudentName,只取出其中一行数据,但是要汇总相等StudentName所有的Grade,谢谢!

[解决办法]
select studentname,sum(grade),max(col1),max(col2),...max(coln)
from tb
group by studentname
[解决办法]
select studentname,col1,col2,..., --写全字段,除了grade
(select sum(grade) from tb where studentname=a.studentname) as grade
from tb a
where not exists (
select 1 from tb
where studentname=a.studentname
and key <a.key
)

读书人网 >SQL Server

热点推荐