1

I have the following tables:

purchase_tbl

id | productId | purchaseQuantity 
---+-----------+-----------------
1  | 1         | 30
2  | 2         | 30
3  | 1         | 10
4  | 2         | 10

sale_tbl

id | productId | saleQuantity
---+-----------+-------------
1  | 1         | 10
2  | 2         | 10
3  | 1         | 10
4  | 2         | 10
5  | 1         | 10
6  | 2         | 10

I need to get the output as this one:

productId | totalPurchasedQuantity| totalSaleQuantity
----------+-----------------------+------------------
1         | 40                    | 30
2         | 40                    | 30

I'm using this query and how to get the desired result?

SELECT purchase_tbl.productId
    , SUM(purchase_tbl.purchaseQuantity) AS totalPurchaseQuantity
    , SUM(sale_tbl.saleQuantity) AS totalSaleQuantity
FROM purchase_tbl
JOIN sale_tbl
    ON purchase_tbl.productId = sale_tbl.productId
GROUP BY purchase_tbl.productId

Current output

productId | totalPurchaseQuantity | totalSaleQuantity
----------+-----------------------+------------------
1         | 120                   | 60
2         | 120                   | 60
11
  • what is the problem with the query, it looks fine to me Commented Jan 25, 2018 at 20:37
  • @Ravi query is fetching two rows with totalPurchasedQuantity as 120 and totalSaleQuantity as 60 Commented Jan 25, 2018 at 20:43
  • can you show your exact output, which you are getting now? Commented Jan 25, 2018 at 20:44
  • 2
    I wouldn't be comfortable assuming every purchased product has a sales product. Commented Jan 25, 2018 at 20:58
  • 1
    @danihp Is a left join is enough? Because products not in the purchase table will not be available for sale. Products in sale_tbl always have an entry in purchase_tbl Commented Jan 26, 2018 at 7:43

2 Answers 2

2

You better group then in separate query, as table have multiple records for each product, which getting cross product.

SELECT purchase.productId, totalPurchaseQuantity, totalSaleQuantity
FROM
(SELECT purchase_tbl.productId
    , SUM(purchase_tbl.purchaseQuantity) AS totalPurchaseQuantity
FROM purchase_tbl
GROUP BY purchase_tbl.productId) purchase
INNER JOIN
(SELECT sale_tbl.productId
    , SUM(sale_tbl.saleQuantity) AS totalSaleQuantity
FROM sale_tbl
GROUP BY sale_tbl.productId
) sale ON sale.productId= purchase.productId;
Sign up to request clarification or add additional context in comments.

10 Comments

FWIW, I'd do the aggregation once, in the super query
@Strawberry But, we don't have unique value in each tables, which causing the wrong output
I can't see that that would matter
@Strawberry hmm.. let me think. Just for side note, i hope, you might have noticed OP has written a sql query in his question (which is similar to what you are saying), but not getting correct output.
I disagree. It's very different
|
0

To obtain your expected result you have to do the aggregation on the individual table before joining them. Your query with be like:

SELECT A.productId, A.totalpurchaseQuantity, B.totalsaleQuantity
FROM
(SELECT productId, SUM(purchaseQuantity) 
   totalpurchaseQuantity FROM purchase_tbl 
    GROUP BY productId) A JOIN 
(SELECT productId, SUM(saleQuantity) 
   totalsaleQuantity FROM sale_tbl 
    GROUP BY productId) B ON
A.productId=B.productId;

2 Comments

thank you :) got the desired result after making a change. Changed id in the first subquery to productId
Ah, I corrected my answer, the idea remains the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.