mysql - SQL SELECT SUM from two tables and group by -
i have 2 tables (po , grn). want get: [this doing:][1] http://sqlfiddle.com/#!9/db000/2
i these results (wrong po_qty , grn_qty , grn_balance ).
what expecting is:
| item | pono | po_qty | grn_qty | grn_balance | |------|------|--------|---------|-------------| | | po1 | 70 | 65 | 5 | | b | po1 | 50 | 0 | 50 | | c | po2 | 10 | 5 | 5 | | d | po3 | 20 | 0 | 20 | | | po4 | 15 | 10 | 5 |
here way of doing it
select p.item, p.pono, sum(p.qty) po_qty, coalesce(g.grn_qty,0) , sum(p.qty) - coalesce(g.grn_qty,0) grn_balance po p left join ( select pono,item,sum(qty) grn_qty grn group pono,item )g on g.pono = p.pono , g.item = p.item group p.item,p.pono order p.pono
Comments
Post a Comment