I have a table Article which has one column keywords which stores tags or keywords
Sample Data
Table Article
------------------------------------------
ID keywords
------------------------------------------
1 one, two, three
2 four, five, six
3 seven, eight, three
4 one, two, three
5 twenty, hundred, one hundred one, one hundred two, seventy
6 seventy, three, two hundred
If I use a CTE query as below then it will concatenate all the keywords column in one row but on other side also gets duplicate row, which is a problem as I we will have 1000’s of article with hundreds of similar keywords.
SELECT TOP 1
stuff(
(
select cast(',' as varchar(max)) + lower(Keywords)
from art_Article a
for xml path('')
), 1, 1, '') AS All_Keywords
FROM
art_Articles G
ORDER BY
G.ArticleKeywords ASC;
Above query yields follow result as single row
---------------------------------------------------
All_Keywords
----------------------------------------------------
one, two, three,four, five, six,seven, eight, three,one, two, three,twenty, hundred, one hundred one, one hundred two, seventy,seventy, three, two hundred
It is obvious from the result that it also show duplicate keywords no. of time as it is stored in row. Is their a way I can get duplicate keywords only once?
I would appreciate if some can help me to sort this to get the result as single column with DISTINCT keywords only.
I think you are going to have to split the data up into rows first, before concatenating it together. This will allow you to get the distinct values when using
FOR XML PATHto get the final list.Here is a CTE version of this process:
See SQL Fiddle with Demo. Which gives the result: