Turn multiple-row MySQL rows into multiple columns and a single row -
the following mysql query
select `fbm_orders`.`order-id`, `fbm_orders`.`purchase-date`, `fbm_orders`.`buyer-name`, `fbm_orders`.`sku`, `im_inv`.`loc_id`, `im_inv`.`qty_on_hnd` `fbm_orders` left join `im_inv` on `fbm_orders`.`sku` = `im_inv`.`item_no` `fbm_orders`.`quantity-to-ship` > 0 order `fbm_orders`.`purchase-date`, `im_inv`.`loc_id` asc;
yields
| order-id | purchase-date | buyer-name | sku |loc_id|qty_on_hnd| |112-9548231-5585051|2017-09-11t21:34:27+00:00|zacchary motz|ie51198|af | 2 | |112-9548231-5585051|2017-09-11t21:34:27+00:00|zacchary motz|ie51198|lo | 1 | |112-9548231-5585051|2017-09-11t21:34:27+00:00|zacchary motz|ie51198|s | 1 | |112-9548231-5585051|2017-09-11t21:34:27+00:00|zacchary motz|ie51198|sl | 0 |
i have looked around on posts similar questions they're using group_concat , gets different result i'm looking for. know how result be
| order-id | purchase-date | buyer-name | sku | af_qty | lo_qty | s_qty | sl_qty | |112-95482..|2017-09-11t21...|zacchary motz|ie51198| 2 | 1 | 1 | 0 |
thank you
for desired result set can use conditional sum of qty_on_hnd
based on loc_id
select o.`order-id`, o.`purchase-date`, o.`buyer-name`, o.`sku`, sum(case when i.`loc_id` = 'af' i.qty_on_hnd else 0 end) af_qty, sum(case when i.`loc_id` = 'lo' i.qty_on_hnd else 0 end) lo_qty, sum(case when i.`loc_id` = 's' i.qty_on_hnd else 0 end) s_qty, sum(case when i.`loc_id` = 'sl' i.qty_on_hnd else 0 end) sl_qty, i.`loc_id`, i.`qty_on_hnd` `fbm_orders` o left join `im_inv` on o.`sku` = i.`item_no` o.`quantity-to-ship` > 0 group o.`order-id`, o.`purchase-date`, o.`buyer-name`, o.`sku` order o.`purchase-date`
Comments
Post a Comment