views:

27

answers:

1

I have a SQL Server DB that has a table of products, and another table which contains a list of the sku variants of each product if it has one.

I want to export all the products and their SKU's into excel. At the moment, I have a helper SQL function which performs the subquery against a product_id and concatenates all the SKU's into a comma-delimited string, e.g:

Product Code,   Name,   SKUs
111             P1      77, 22, 11

Is there an easier way to do this, so that each SKU is a row which the associated product code as well, i.e:

Product Code, Name, SKUs
111           P1    77
111           P1    22
111           P1    11
A: 

Assuming the two tables are linked by a product_code column, being in both tables:

SELECT p.product_code,
       p.name,
       sv.sku
  FROM PRODUCTS p
  JOIN SKU_VARIANTS sv ON sv.product_code = p.product_code
OMG Ponies