求一个sql server语句

表1状态为1的数据左连接表2

表2名字相同的数字合并一起有for xml path,分开各成一列没研究过。。应该要应用程序处理才行了。。

--如果你的d字段内容为数字需要使用str(d)转字符串,要不会报错
select c,STUFF((select ','+d from t2 where t2.c=t2_1.c for xml path('')),1,1,'') from t2 t2_1
group by c

select t1.a ,t2.d from t1 left join t2 on t1.a = t2.c where t1.b=1  group by t1.a