Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
lyndontrm
Starting Member
1 Post |
Posted - 2012-08-21 : 11:04:16
|
| Hello everyone,I am a newbie to sql and I am faced with the problem below.I have 3 tablesCategoriesCat_ID | Cat_Name1 | Cosmetics2 | Hair3 | Furniture4 | OfficeProductsProd_ID | Prod_name1 | Chair2 | GelProduct_categoriesProd_ID | Cat_id1 | 31 | 42 | 12 | 2I need a query that will give me the followingProduct | CategoriesChair | Furniture , OfficeGel | Cosmetics, HairAny help would be greatly appreciatedRegards,Lyndon |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-21 : 11:10:12
|
| [code];With ProdCatAS(SELECT p.Prod_Name,c.Cat_NameFROM Product pINNER JOIN Product_categories pcON pc.Prod_ID = p.Prod_IDINNER JOIN Categories cON c.Cat_ID = pc.Cat_ID)SELECT p.Prod_name AS Product,STUFF((SELECT ',' + Cat_Name FROM ProdCat WHERE Prod_Name = p.Prod_Name ORDER BY Cat_ID FOR XML PATH('')),1,1,'') AS CategoriesFROM (SELECT DISTINCT Prod_name FROM ProdCat)p[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|