I have this dataset with 4 tables. I am trying to write the SQL query as following:
WITH test AS
(
SELECT
(f.name), f.id, f.domain, s.link,
(SELECT
name,
CASE
WHEN name IN (1, 3, 8) THEN 1
WHEN name IN (2, 6, 7) THEN 2
END AS [group]
FROM tags
WHERE corporate_statement_link_id = s.id
FOR JSON PATH) AS tags
FROM
fortune1000_companies f
LEFT JOIN
search_results s ON f.id = s.company_id
LEFT JOIN
corporate_statements c ON s.id = c.corporate_statement_link_id
WHERE
c.corporate_statement = 1
AND s.domain LIKE CONCAT('%', f.domain, '%')
)
SELECT name, link, tags
FROM test
but this produces the result where company names are duplicated because of the differences in link. For e.g., UnitedHeath Group (rows 4 & 5) is in two rows because the link is different. I want the result in such a way that the company name is shown just once, and tags are in the same group together. I don't need link to be shown; only included for this SO.