日期:2014-05-17 浏览次数:20634 次
create table course (name varchar(16), credit int) insert into course select '英语1', 50 union all select '英语2', 30 union all select '英语3', 20 union all select '物理1', 50 union all select '物理2', 20 union all select '化学', 40 union all select '形势与政治1', 50 union all select '形势与政治2', 40 with t as (select case isnumeric(right(name,1)) when 1 then left(name,len(name)-1) else name end 'subjectname', name,credit from course ) select a.subjectname+ stuff((select '、'+replace(name,a.subjectname,'') from t b where b.subjectname=a.subjectname for xml path('')),1,1,'') 'name', sum(a.credit) 'credit' from t a group by a.subjectname order by a.subjectname desc; /* name credit ------------------ ----------- 英语1、2、3 100 形势与政治1、2 90 物理1、2 70 化学 40 (4 row(s) affected) */