sql分組拼接字段


 


--聯查
select n.*,t.Name from News n join Type_News tn on n.Id=tn.NId join Types t on t.Id=tn.TId

--拼接並插入臨時表
select aaa.Title,aaa.Content, stuff((select ',' + ttt.Name from (select n.*,t.Name from News n join Type_News tn on n.Id=tn.NId join Types t on t.Id=tn.TId ) as ttt for xml path('')),1,1,'') as field into #T from (
select n.* from News n join Type_News tn on n.Id=tn.NId join Types t on t.Id=tn.TId
) aaa

drop table #T

select * from #T

 


--分組查詢
select ROW_NUMBER() over(order by Title) as rowId, * from #t group by Title,content,field

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM