Take a look here:
need sql query for some action: rows into single column depending on other column[
^]
If you need an example, you need to put columns and tables names.
[EDIT]
I've done an example. How to test it?
1) Copy code below.
2) Open MS SQL Server Management Studio and click "New query" button
3) Paste code from clipboard
4) Push "Execute" button
IF NOT OBJECT_ID(N'#Products') IS NULL DROP TABLE #Products
CREATE TABLE #Products (Category NVARCHAR(10), ShortName NVARCHAR(10), CodeId NVARCHAR(30), Manufacturer NVARCHAR(50))
INSERT INTO #Products (Category, ShortName, CodeId, Manufacturer)
VALUES('xx', 'C201', '02300877','Samsung')
INSERT INTO #Products (Category, ShortName, CodeId, Manufacturer)
VALUES('xx', 'C201','02300877' ,'Toshiba')
INSERT INTO #Products (Category, ShortName, CodeId, Manufacturer)
VALUES('xx', 'C213', '02300877','Samsung')
INSERT INTO #Products (Category, ShortName, CodeId, Manufacturer)
VALUES('xx', 'C213', '02300877','Toshiba')
INSERT INTO #Products (Category, ShortName, CodeId, Manufacturer)
VALUES('xx', 'C606', '02300877','Samsung')
INSERT INTO #Products (Category, ShortName, CodeId, Manufacturer)
VALUES('xx', 'C606', '02300877','Toshiba')
INSERT INTO #Products (Category, ShortName, CodeId, Manufacturer)
VALUES('xx', 'C303', '02301163','Toshiba')
INSERT INTO #Products (Category, ShortName, CodeId, Manufacturer)
VALUES('xx', 'C305', '02301163','Toshiba')
INSERT INTO #Products (Category, ShortName, CodeId, Manufacturer)
VALUES('xx', 'C712', '02301163','Samsung')
INSERT INTO #Products (Category, ShortName, CodeId, Manufacturer)
VALUES('xx', 'C207', '02301165','Samsung')
INSERT INTO #Products (Category, ShortName, CodeId, Manufacturer)
VALUES('xx', 'C209', '02301165','Philips')
INSERT INTO #Products (Category, ShortName, CodeId, Manufacturer)
VALUES('xx', 'C708', '02301165','Philips')
SELECT t1.[Category], STUFF( (SELECT ',' + [ShortName] AS 'text()'
FROM #Products t2
WHERE t2.CodeId = t1.CodeId
FOR XML PATH('')), 1, 1, '') AS [Keywords], t1.[CodeId], t1.[Manufacturer]
FROM #Products t1
GROUP BY t1.[Category], t1.[CodeId], t1.[CodeId], t1.[Manufacturer]
DROP TABLE #Products
Result:
Category | Keywords | CodeId | Manufacturer |
---|
xx | C201,C201,C213,C213,C606,C606 | 02300877 | Samsung |
xx | C201,C201,C213,C213,C606,C606 | 02300877 | Toshiba |
xx | C303,C305,C712 | 02301163 | Samsung |
xx | C303,C305,C712 | 02301163 | Toshiba |
xx | C207,C209,C708 | 02301165 | Philips |
xx | C207,C209,C708 | 02301165 | Samsung |
I hope it will be helpful...
[/EDIT]