Export products and variants from SQL Server
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 2开发者_开发百科2
111 P1 11
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
精彩评论