please provide me sql query for below output sum query please help me

93852217_246197733117734_2443195689677619200_n.jpg

Recommended Answers

All 4 Replies

Probably something like this:

SELECT * 
FROM (
    SELECT product_id, SUM(quantity) AS purchase
    FROM purchase 
    GROUP BY product_id
) P
JOIN (
    SELECT product_id, SUM(quantity) AS sale
    FROM sale
    GROUP BY product_id
) S ON S.product_id = P.product_id

please any one help me

commented: If you need more help, explain what's wrong with my solution. +15

Dear sir please help me, here product id 4 is not entry to sale column but i want to show purchase column result as like below picture. sir please help be as before code. thanks

22222.jpg

You can use Union All Clause.

Your sql syntax should be

SELECT tt.product_id, tt.purchase, tt.sale
FROM (SELECT product_id, SUM(quantity) AS purchase, SUM(0) AS sale FROM purchase GROUP BY product_id) 
UNION ALL (SELECT product_id, SUM(0) AS purchase, SUM(quantity) AS sale FROM sale GROUP BY product_id) tt
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.