放假了依然加班求个sql语句
年级       班级
一年级      1
一年级      2
一年级      3
一年级      4
二年级      1
二年级      2
二年级      3
二年级      4
要求查询出结果为
年级       班级
一年级      1,2,3,4
二年级      1,2,3,4
              
              
------解决方案--------------------
select 年级,
stuff((select ','+班级 from table where 年级=a.年级 for xml path('')),1,1,'') as 班级
from table as a group by 年级
如果年级是int型的话,与字符拼接时要转换类型
cast(班级 as nvarchar(20))