22,209
社区成员
发帖
与我相关
我的任务
分享
Select JBXX.bh,JBXX.xm,YZJC.jclb from JBXX join YZJC on JBXX.bh=YZJC.bh Order by JBXX.bh asc
with jbxx(bh,xm) as (
select 1,'张一' union all
select 2,'张二' union all
select 3,'张三' union all
select 4,'张四' union all
select 5,'张五'),yzjc(bh,jclb) as
(select 1,'表扬' union all
select 1,'记功' union all
select 2,'优秀' union all
select 3,'优秀' union all
select 3,'表扬' union all
select 4,'优秀' union all
select 5,'表扬')
select a.bh,a.xm,stuff((select ','+b.jclb from yzjc as b
where a.bh=b.bh for xml path('')),1,1,'') as jclb
from jbxx as a
/*
bh xm jclb
1 张一 表扬,记功
2 张二 优秀
3 张三 优秀,表扬
4 张四 优秀
5 张五 表扬
*/
select bh,xm,jclb=stuff((select ' '+jclb+';' from yzjc a where a.bh=b.bh for xml path('')),1,1,'')
from jbxx b
group by b.bh