pivot - Group by with UNION in Mariadb -
i have query:
select item_code, item_name, first, second, third, `fourth` ( (select t.item_code, i.item_name, t.actual_qty `first`, '' `second`, '' `third`, '' `fourth` tabbin t join `tabitem` on i.name = t.item_code t.warehouse = "finished goods") union (select t.item_code, i.item_name, '' `first`, t.actual_qty `second`, '' `third`, '' `fourth` tabbin t join `tabitem` on i.name = t.item_code t.warehouse = "tank 01 - 1200 kg") union (select t.item_code, i.item_name, '' `first`, '' `second`, t.actual_qty `third`, '' `fourth` tabbin t join `tabitem` on i.name = t.item_code t.warehouse = "tank 02 - 1200 kg") union (select t.item_code, i.item_name, '' `first`, '' `second`, '' `third`, t.actual_qty `fourth` tabbin t join `tabitem` on i.name = t.item_code t.warehouse = "tank 03 - 1200 kg")) temp group temp.item_code, temp.item_name, temp.first, temp.second, temp.third, temp.fourth and output:
item_code item_name first, second, third, fourth fg-plu plum 30.000000 fg-plu plum 40.000000 fg-plu plum 10.000000 fg-plu plum 1248.00 i want group item_code , item_name or item_code.
this appears pivot query in disguise. use max case expressions output want:
select t.item_code, i.item_name, max(case when t.warehouse='finished goods' t.actual_qty end) first, max(case when t.warehouse='tank 01 - 1200 kg' t.actual_qty end) second, max(case when t.warehouse='tank 02 - 1200 kg' t.actual_qty end) third, max(case when t.warehouse='tank 03 - 1200 kg' t.actual_qty end) fourth tabbin t inner join tabitem on i.name = t.item_code group t.item_code, i.item_name
Comments
Post a Comment