Reputation: 1100
So I have specification tables that relate to products that vary by product type. I need a stored procedure that will return a single-row of unique comma-delimited values in each of the columns (used for site navigation code) in a spec table (or a resultset of said spec table).
I hope this makes sense.
Thank you all for the responses so far. I will explain in more detail...
Each spec table has a different number of columns (and rows, for that matter). Let's say one of them has 50,000 rows and a manufacturer column. Now let's assume there are 5 unique manufacturers in the there as well. Let's also assume the same table also has a color column that shares 7 colors among all 50,000 rows.
My (currently imaginary) stored procedure should return one row with the following columns...
I intend to use this result to build my site navigation.
Upvotes: 0
Views: 217
Reputation: 24430
Assuming I've understood:
declare @productTypes table (Id int, Name nvarchar(32))
declare @products table (Id bigint not null identity(1,1), ProductType int, ProductName nvarchar(32))
insert @productTypes (Id, Name)
select 1, 'Food'
union
select 2, 'Drink'
insert @products (ProductType, ProductName)
select 1, 'Chips'
union
select 1, 'Fish'
union
select 2, 'Guinness'
union
select 2, 'Water'
union
select 2, 'Pan Galactic Gargle Blaster'
select pt.Name
, STUFF
(
(
Select ',' + a.ProductName
from @products a
where a.ProductType = pt.Id
FOR XML PATH('')),1,1,''
) ProductsOfType
from @productTypes pt
SQL Fiddle: http://sqlfiddle.com/#!6/e80ff/1
Results:
Name ProductsOfType
Food Chips,Fish
Drink Guinness,Pan Galactic Gargle Blaster,Water
Upvotes: 1