Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: How do I SUM non duplicates values when doing multiples JOINS

I try to SUM values from columns, from a query which contains some JOINS.

Example:

SELECT
    p.id AS product_id,
    SUM(out_details.out_details_quantity) AS stock_bought_last_month,
    SUM(order_details.order_quantity) AS stock_already_commanded
FROM product AS p 
INNER JOIN out_details ON out_details.product_id=p.id 
INNER JOIN order_details ON order_details.product_id=p.id 
WHERE p.id=9507
GROUP BY out_details.out_details_pk, order_details.id;

I get this result :

+------------+-------------------------+-------------------------+
| product_id | stock_bought_last_month | stock_already_commanded |
+------------+-------------------------+-------------------------+
|       9507 |                      22 |                      15 |
|       9507 |                      22 |                      10 |
|       9507 |                      10 |                      15 |
|       9507 |                      10 |                      10 |
|       9507 |                       5 |                      15 |
|       9507 |                       5 |                      10 |
+------------+-------------------------+-------------------------+

Now, I want to SUM the values, but of course there are duplicates. I also have to group by product_id :

SELECT 
  p.id AS product_id,
  SUM(out_details.out_details_quantity) AS stock_bought_last_month,
  SUM(order_details.order_quantity) AS stock_already_commanded
FROM product AS p 
INNER JOIN out_details ON out_details.product_id=p.id 
INNER JOIN order_details ON order_details.product_id=p.id 
WHERE p.id=9507
GROUP BY p.id;

Result :

+------------+-------------------------+-------------------------+
| product_id | stock_bought_last_month | stock_already_commanded |
+------------+-------------------------+-------------------------+
|       9507 |                      74 |                      75 |
+------------+-------------------------+-------------------------+

The result wanted is :

+------------+-------------------------+-------------------------+
| product_id | stock_bought_last_month | stock_already_commanded |
+------------+-------------------------+-------------------------+
|       9507 |                      37 |                      25 |
+------------+-------------------------+-------------------------+

How do I ignores duplicates? Of course, the count of lines can change!

like image 432
frinux Avatar asked Jul 27 '10 16:07

frinux


People also ask

How can we avoid duplicate records in SQL while joining two tables?

Solution. Select column values in a specific order within rows to make rows with duplicate sets of values identical. Then you can use SELECT DISTINCT to remove duplicates. Alternatively, retrieve rows in such a way that near-duplicates are not even selected.

How do I exclude duplicate values in mysql?

Eliminating Duplicates from a Query Resultmysql> SELECT DISTINCT last_name, first_name -> FROM person_tbl -> ORDER BY last_name; An alternative to the DISTINCT command is to add a GROUP BY clause that names the columns you are selecting.

How do you avoid including duplicate values in a query result?

The go to solution for removing duplicate rows from your result sets is to include the distinct keyword in your select statement. It tells the query engine to remove duplicates to produce a result set in which every row is unique.


1 Answers

Select P.Id
    , Coalesce(DetailTotals.Total,0) As stock_bought_last_month
    , Coalesce(OrderTotals.Total,0) As stock_already_commanded
From product As P
    Left Join   (
                Select O1.product_id, Sum(O1.out_details_quantity) As Total
                From out_details As O1
                Group By O1.product_id
                ) As DetailTotals
        On DetailTotals.product_id = P.id
    Left Join   (
                Select O2.product_id, Sum(O2.order_quantity) As Total
                From order_details As O2
                Group By O2.product_id
                ) As OrderTotals
        On OrderTotals.product_id = P.id
Where P.Id = 9507   
like image 60
Thomas Avatar answered Oct 26 '22 18:10

Thomas