DECLARE @filter TABLE (id INT IDENTITY, data nvarchar(32)) insert into @filter (data) values ('sql,performance') insert into @filter (data) values ('profiler') insert into @filter (data) values ('table,performance,view')
DECLARE @blog TABLE (id INT IDENTITY, name nvarchar(20), tags nvarchar(20)) insert into @blog (name,tags) values ('post 1','sql,profiler,table') insert into @blog (name,tags) values ('post 2','sql,performance') insert into @blog (name,tags) values ('post 3','profilter') insert into @blog (name,tags) values ('post 4','view,table')
;with cte as( select n=1, id, tag=case charindex(',',tags) when 0 then tags else substring(tags,1,charindex(',',tags,1)-1) end , txt=substring(tags,charindex(',',tags,2)+1,len(tags)) from @blog union all select n=n+1, id, tag=case charindex(',',txt) when 0 then txt else substring(txt,1,charindex(',',txt,1)-1) end , txt=substring(txt,charindex(',',txt,2)+1,len(txt)) from cte where txt!=tag ) select f.id, f.data, b.[name], b.tags, RELEVANCE=count(*) from cte join @blog b on b.id=cte.id join @filter f on charindex(','+cte.tag+',' , ','+f.data+',')>0 group by f.id, f.data, b.[name], b.tags order by id, count(*)desc, b.[name]
insert into @filter (data) values ('sql,performance')
insert into @filter (data) values ('profiler')
insert into @filter (data) values ('table,performance,view')
DECLARE @blog TABLE (id INT IDENTITY, name nvarchar(20), tags nvarchar(20))
insert into @blog (name,tags) values ('post 1','sql,profiler,table')
insert into @blog (name,tags) values ('post 2','sql,performance')
insert into @blog (name,tags) values ('post 3','profilter')
insert into @blog (name,tags) values ('post 4','view,table')
;with cte
as( select n=1,
id,
tag=case charindex(',',tags)
when 0 then tags
else substring(tags,1,charindex(',',tags,1)-1) end ,
txt=substring(tags,charindex(',',tags,2)+1,len(tags))
from @blog
union all
select n=n+1,
id,
tag=case charindex(',',txt)
when 0 then txt
else substring(txt,1,charindex(',',txt,1)-1) end ,
txt=substring(txt,charindex(',',txt,2)+1,len(txt))
from cte
where txt!=tag
)
select
f.id,
f.data,
b.[name],
b.tags,
RELEVANCE=count(*)
from
cte join
@blog b on b.id=cte.id join
@filter f on charindex(','+cte.tag+',' , ','+f.data+',')>0
group by
f.id,
f.data,
b.[name],
b.tags
order by
id,
count(*)desc,
b.[name]
To Top