I'm trying to combine the results of multiple rows into one. I would like to flatten the first two rows below, and perhaps combine the keywords so that they are in the same column. How can I do that simply using a select statement (i.e. no functions)?
I'm currently getting:
documentid documentname keyword
1 doc1 politics politics italy
2 doc2 politics politics italy
I would like to get:
documentid documentname keyword
1 doc1 politics italy
2 doc2 politics
This is part of my query:
SELECT d.DocumentId AS documentid ,
m.Title AS documentname ,
STUFF(( SELECT N' ' + k.Word
FROM [arabicarchive].[dbo].[Keywords] k
JOIN [arabicarchive].[dbo].DocumentKeywords dk ON k.KeywordId = dk.Keyword_KeywordId
JOIN [arabicarchive].[dbo].Documents d ON dk.Document_DocumentId = d.DocumentId
FOR
XML PATH('')
), 1, 1, '') AS Keyword
FROM [arabicarchive].[dbo].[Metadatas] m
JOIN [arabicarchive].[dbo].[Documents] d ON d.DocumentId = m.DocumentId
WHERE d.Status = 1
EDIT: I have updated the query and the results that I am currently getting. I haven't used STUFF or XML PATH before so please bear with me.
EDIT 2: I have managed to get rid of the duplicate row, but the result in the keyword column is not correct.
EDIT 3: Adding DISTINCT
to the query still doesn't produce a keyword column with the correct values.