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 

http://sqlfiddle.com/#!9/db000/30


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -