
--统计每个班中超过22岁的男同学信息
select ClassID,COUNT(1) as '超过22岁个数',
(select Name+',' from #PersonInfo where ClassID=p.ClassID for xml path ('')) as '姓名集合'
from #PersonInfo p where Sex='男' and Age>22 group by ClassID order by ClassID
select ClassID,Age,COUNT(1) as '超过22岁个数',
(select Name+',' from #PersonInfo where ClassID=p.ClassID and Age=p.Age for xml path ('')) as '姓名集合'
from #PersonInfo p where Sex='男' and Age>22 group by ClassID,Age order by ClassID
select ClassID,Age,COUNT(1) as '超过22岁个数',
(select Name+',' from #PersonInfo where ClassID=p.ClassID and Age=p.Age for xml path ('')) as '姓名集合'
from #PersonInfo p where Sex='男' group by ClassID,Age having Age>22 order by ClassID
原文:http://blog.csdn.net/richard_dzh/article/details/46441861