sql 一列拼接成一行,再分割成列

 

原始数据,需要拼接成一行

 

select * from (select d.*,(SELECT count ([Keyword])FROM [DragonGuoShi].[dbo].[ArticleInfo]where Keyword like'%'+d.Keyword+'%' and [ArticleCreateDate]>'2018-06-10') as Num from (select distinct(Keyword) from( select SUBSTRING( Keyword,number,CHARINDEX(',',Keyword+',',number)-number) as Keyword from( SELECT stuff((SELECT ','+[Keyword] from (SELECT [Keyword]FROM [DragonGuoShi].[dbo].[ArticleInfo]where Keyword!='' and [ArticleCreateDate]>'2018-06-10')a for xml path('')),1,1,'') as Keyword)b ,master..spt_values where
number >=1 and number<=len(Keyword)
and type='p'
and substring(','+Keyword,number,1)=',') c ) d ) e where Num>20 order by Num desc

 结果

 

posted @ 2018-07-06 15:03  Mr.石  阅读(486)  评论(0编辑  收藏  举报