日期:2014-05-17 浏览次数:20451 次
insert into b(id,sname,cname,score)
select id=row_number() over(order by getdate()),
sname,cname,max(score) as score
from tb
group by sname,cname
with tb(id ,Sname,Cname , Score , CreateTime ,Other )
as
(select 1, '张三','语文',88,'2010-01-01','' union all
select 2, '张三','数学',97,'2010-01-01','' union all
select 3,'张三', '数学',98,'2010-01-01','' union all
select 4,'李四', '语文',84,'2010-01-01','' union all
select 5, '李四', '语文', 88,'2010-01-01',''
)
select * from tb tb1 where id in(select id from tb tb2 where tb1.Sname=tb2.Sname and tb1.Cname=tb2.Cname and tb2.Score=
(select max(Score) from tb tb3 where tb3.Sname=tb2.Sname and tb2.Cname=tb3.Cname))