I have a table with data of the duplicate result set, like the column names and the values which caused the duplicates.
| ColName | ColValue | UniqueIDpercombination |
|---|---|---|
| KitiD | K89901 | 1 |
| Kit | 00900 | 1 |
| KitiD | L7865 | 2 |
| Kit | 00400 | 2 |
| UPC | 345234 | 3 |
| UPN | AVF | 3 |
...... and so on.
I would like to combine the colname and colvalues into a single row per uniqueID as shown in below table. The column name values are not fixed.
| ColName | ColValue | UniqueIDpercombination |
|---|---|---|
| KitiD - Kit | K89901 - 00900 | 1 |
| KitiD - Kit | L7865 - 00400 | 2 |
| UPC - UPN | 345234 AVF | 3 |
I tried using stuff with xml path, I did not get the desired output, below is the query which I tried and the output.
SELECT STUFF
(
(
SELECT '-' + s.Colname +','
FROM ##resultset s
--group by uniqueidpercombination, colname
FOR XML PATH('')
),
1, 1, ''
) AS colname
,STUFF
(
(
SELECT '/' + s.colvalue
FROM ##resultsets
--group by uniqueidpercombination, colvalue
FOR XML PATH('')
),
1, 1, ''
) AS colvale
| ColName | ColValue |
|---|---|
| kitid-kit-kitid-kit | K89901-00900 -L7865-00400 |
Any suggestions on how to fix this?.