Here is an example of my tables:
the details
+----------+---------------+
| txt_item | txt_unique_id |
+----------+---------------+
| Circle | 1 |
| Square | 2 |
| Triangle | 3 |
+----------+---------------+
tag_master
+---------+----------+
| txt_tag | opt_type |
+---------+----------+
| red | color |
| blue | color |
| yellow | color |
| large | size |
| medium | size |
| small | size |
+---------+----------+
item_tags
+---------+---------------+
| txt_tag | txt_unique_id |
+---------+---------------+
| red | 1 |
| blue | 1 |
| large | 1 |
| small | 1 |
| red | 2 |
| yellow | 2 |
| small | 2 |
| medium | 2 |
| red | 3 |
| yellow | 3 |
+---------+---------------+
I want to return this:
+----------+----------------------------+
| Circle | red, blue, large, small |
| Square | red, yellow, small, medium |
| Triangle | red, yellow |
+----------+----------------------------+
This is what I get:
+----------+---------------------------------------------+
| Circle | red, red, red, blue, large, small, small |
| Square | red, red, red, yellow, yellow, small, small |
| Triangle | red, red, red, yellow, yellow |
+----------+---------------------------------------------+
This is where I am located:
CREATE TABLE
(
txt_unique_id VARCHAR(36),
tags VARCHAR(1000)
)
INSERT INTO
(txt_unique_id,
tags)
(SELECT txt_unique_id,
( STUFF((SELECT ' , ' + t.txt_tag
FROM item_tags t
JOIN tag_master_ tm
ON t.txt_tag = tm.txt_tag
JOIN items i
ON t.txt_unique_id = i.txt_unique_id
ORDER BY opt_type,
txt_tage
FOR xml path('')), 1, 1, '') )
FROM item_tags t)
SELECT *
FROM
I also tried using COALESCE, but I am missing something. I need DISTINCT or something else, but everything I tried does not work. I cannot use DISTINCT or TOP 1 if I want to ORDER opt_type. Appreciate the help.