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


Recommended Answers

All 4 Replies

Probably something like this:

    SELECT product_id, SUM(quantity) AS purchase
    FROM purchase 
    GROUP BY product_id
) P
    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


You can use Union All Clause.

Your sql syntax should be

SELECT tt.product_id, tt.purchase,
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, learning, and sharing knowledge.