I have three tables: Products, Purchase, Invoice
Product table:
Producct_no Name
1 A
2 B
3 C
Purchase table:
Purchase_no Product_no Qty
001 1 81
002 2 150
003 3 10
Invoice table:
Invoice_no Product_no Qty
001 1 20
002 2 10
003 3 10
I want to get each product's purchase quantity and invoice quantity, I used following query
SELECT PR.product_no, sum(P.qty),sum(I.qty) FROM products PR LEFT JOIN invoice I ON I.product_no=PR.product_no LEFT JOIN purchase P ON P.product_no=PR.product_no group by PR.product_no
product_no sum(P.qty) sum(I.qty)
001 162 160
002 150 50
003 10 10
EDIT: Expected results
product_no sum(P.qty) sum(I.qty)
001 81 20
002 150 10
003 10 10
My query is giving me wrong response (sum of quantities are wrong), please help me to correct my query to get the results properly. thanks
sum(P.qty)
twice. Also It's clear what you want; each product only exists once on each table, so it is not clear why you want the sum, rather than the result? – nomistic