razin223 Posted January 3, 2011 Share Posted January 3, 2011 Table - SELL ------------------------------ | model | quantity | ---------------------------- | C | 500 | | B | 500 | | C | 300 | | B | 200 | ----------------------------- Table - BUY ------------------------------ | model | quantity | ------------------------------- | A | 2000 | | B | 2000 | | C | 2000 | | A | 1000 | ------------------------------- I need to SUM of SELL and BUY both Group By. Then I need to subtract them as (BUY - SELL) so I can have the current status of my stock. Just like this ---------------------------- | model | quantity | ------------------------------- | A | 3000 | | B | 1300 | | C | 1200 | --------------------------------- Now how can I do it?My Mysql version is 5.1 Quote Link to comment Share on other sites More sharing options...
joel24 Posted January 4, 2011 Share Posted January 4, 2011 something along these lines SELECT b.model, SUM(b.quantity) - SUM(s.quantity) AS currentQuantity FROM sell s JOIN buy b ON s.model=b.model GROUP BY b.model Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.