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

本文介绍了一个复杂的SQL查询案例,该查询旨在从特定数据库中筛选出关键词出现次数超过20次的文章信息,并按出现频率降序排列。涉及的技术包括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

 结果

 

转载于:https://www.cnblogs.com/shiyh/p/9273686.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值