通过sql的stuff 把一列几行的记录拼接在一行一个字段

---通过sql的stuff 把一列几行的记录拼接在一行一个字段

select FID,a.FCustomerID as 工地ID
        , 应验收节点 = (stuff((select ',' + isnull(t2.FName,'') from MD_ProjectPlanEntry t1 
                            inner join MD_BaseData6_L t2 on t1.FGCJDXM=t2.FID 
                            where t1.FID=a.FID and convert(varchar(6),t1.FPEDATE,112)<=convert(varchar(6),getdate(),112)  
                            and FAEDATE is null
                        for xml path('')),1,1,'')) 
        , 实际验收节点 = (stuff((select ',' + t2.FName from MD_ProjectPlanEntry t1 
                            inner join MD_BaseData6_L t2 on t1.FGCJDXM=t2.FID 
                            where t1.FID=a.FID and convert(varchar(6),FAEDATE,112)=convert(varchar(6),getdate(),112)  
                        for xml path('')),1,1,'')) 
        from MD_ProjectPlan a 
       group by a.FID ,a.FCustomerID

 

你可能感兴趣的:(通过sql的stuff 把一列几行的记录拼接在一行一个字段)