sql - How to turn a order items into a column list of products -
i trying list of products order listed columns. tried pivot table dont want column names of product static names product 1, product 2 , row have product sku , quantity. same products should go in same column. sku1 in product1 column etc
example. order , orderitems table. output like
header:
orderid,product1, quantity1,product2 quantity2,product3,quantity3
rows:
1 , sku1,3, sku2,4, sku3,5
2 , sku1,2, sku2,5, sku3,1
i have created sql fiddle: http://sqlfiddle.com/#!3/d993a/4/0 example of structure , select.
my last thought table variable revant columns , insert still cant see best option.
thanks in advance.....
select b.quantity, case when b.productname='product 1' b.sku end 'product1', case when b.productname='product 2' b.sku end 'product2', case when b.productname='product 3' b.sku end 'product3', case when b.productname='product 4' b.sku end 'product4', case when b.productname='product 5' b.sku end 'product5', case when b.productname='product 6' b.sku end 'product6' order1 join orderitems b on (a.orderid=b.orderid) a.orderid= b.orderid ;
output:
+----------+----------+----------+----------+----------+----------+----------+ | quantity | product1 | product2 | product3 | product4 | product5 | product6 | +----------+----------+----------+----------+----------+----------+----------+ | 2 | sku1 | null | null | null | null | null | | 4 | null | sku2 | null | null | null | null | | 5 | null | null | sku3 | null | null | null | | 2 | null | null | null | sku4 | null | null | | 1 | null | null | null | null | sku5 | null | | 2 | sku1 | null | null | null | null | null | | 4 | null | sku2 | null | null | null | null | | 5 | null | null | sku3 | null | null | null | | 2 | null | null | null | sku4 | null | null | | 1 | null | null | null | null | sku5 | null | | 1 | null | null | null | null | null | sku6 | +----------+----------+----------+----------+----------+----------+----------+
Comments
Post a Comment