Join creates duplicate rows - mysql

I'm trying to join 3 tables in BigQuery that contain sales for products with the same identifier (SKU) and attending to the order date.
My goal would be to have in one table the order date, Sales Table 1, Sales Table 2, Sales Table 3 and Product identifier (SKU). For that I've been trying the next query:
SELECT
Order_Date, Sales1, Sales2, Sales3, SKU
FROM
Table 1
LEFT JOIN
Table 2 ON Order_Date_Table1 = Order_Date_Table2 AND SKU_Table1 = SKU_Table2
LEFT JOIN
Table 3 ON Order_Date_Table1 = Order_Date_Table3 AND SKU_Table1 = SKU_Table3
However when 2 of the tables contain sales for the same SKU and the same day, I get duplicated rows. Something similar happens with all other join types.
How can I avoid this?

It seems to me that aggregation can be a solution for your problem:
SELECT Order_Date, SKU, SUM(Sales1), SUM(Sales2), SUM(Sales3)
...
GROUP BY Order_Date, SKU

Related

Calculate Count of items summed up in a Group By Query

I have two tables:
Orders
======
id total_price created_on
1 100 2021-01-22
2 200 2021-01-23
Items
=====
id order_id
11 1
12 1
13 2
I want to create a query to get revenue by date. For this i'm going to sum up total price in order and grouping it up by date. Along with revenue, I also want to get total numbers of orders and items for that date. Here's a query that I wrote:
SELECT
count(orders.id) as orders,
sum(orders.total_price) as billing,
DATE(CREATED_ON) as created_on
FROM
orders
WHERE orders.deleted_on IS NULL
group by Date(orders.created_on);
Now I found 2 problems:
The count of orders is coming incorrect. Not sure what. i'm doing wrong here.
How can I calculate the count of items also in same query ?
I'm learning sql and this seems a big difficult to get my head around. Thanks for your help.
As Items.order_id is foreign key to Order.id as a result we need to join both tables first.
SELECT count(order_id) AS orders,sum(total_price) AS billing,Orders.created_on as created_on FROM Orders,(select order_id from Items) as new WHERE Orders.id=new.order_id GROUP BY created_on;
This is tricky, because when you combine the items you might multiple the revenue. One method is to aggregate the items before joining to orders:
SELECT DATE(o.Created_On) as created_on_date,
COUNT(*) as num_orders,
SUM(i.num_items) as num_items,
SUM(o.total_price) as billing
FROM orders o LEFT JOIN
(SELECT i.order_id, COUNT(*) as num_items
FROM items i
GROUP BY i.order_id
) i
ON i.order_id = o.id
WHERE o.deleted_on IS NULL
GROUP BY DATE(o.created_on);
Note: This uses a LEFT JOIN because you have not specified that all orders have items. If all do then an INNER JOIN would suffice.

count rows in one table based on the id's of the other table

I have two tables:
Orders
Orders product
I currently have the query that counts the amounts of orders:
SELECT COUNT(*) as total FROM orders WHERE DATE(`created_on`) = CURDATE()
This gives me the amount of orders from today
Now I want to change this query, so insted of that I count the orders I count the amount of products that they ordered.
The order_products table is like this:
-id
-order_id
-product_name
-product_weight
etc etc
I am only interested in the order_id because that links this table to the orders table. For every product that is ordered, 1 row is added in this table linked to the order with the order_id.
Is it possible to have one single query to select the count of that, or is that not possible? I think I have to use a LEFT JOIN for this operation, but I cannot seem to find it.
You just need to create inner join between those table and applying the relation throw foreign key then you need to add group by condition on what you are looking for
SELECT COUNT(*) as total FROM orders o inner join order_product op on(op.order_id=o.order_id) WHERE DATE(`created_on`) = CURDATE() group by op.id

can't make the sum of columns in 2 connected tables using MySQL

I am quit new to MySQL.
I have 2 tables:
Table 1: client_debts (id, client_name, total_debts)
Table 2: client_details (id, client_id, payments)
Where client_id is the foreign key of table 2 and is connected to table 1 id column.
What I need to do is, to create an PHP-HTML (No problem with this part) table that gives me the total debts of a client, with the total of his payments to compare how much he still have. IF his payments are equal, I want to remove his name from the HTML table.
What I tried ?
I tried this SQL query:
SELECT (SELECT sum(total_debts) FROM client_debts) AS 'TOTAL',
(SELECT sum(payments) FROM client_details) AS 'Total Payed'
FROM client_debts INNER JOIN client_details
ON client_debts.id = client_details.client_id
And I have got this:
And the results are only for the first id and it is duplicated to all rows.
So how can I do this for every client?
Try This Query
SELECT client_debts.id,sum(total_debts) AS 'TOTAL',
sum(payments) AS 'Total Payed'
FROM client_debts
INNER JOIN client_details
ON client_debts.id = client_details.client_id
GROUP BY client_debts.id;
I hope this will work and in this query get all client id and its SUM.

mysql - Using Group By

I am trying to write a mysql query for an app I'm developing for android.
I have a database that has a bill_content table and a products table
I want to select top 10 most sold products.
This is a minimal version of what I have, but it's all I need to get an answer here.
bill_content table has the columns: id, id_product, quantity (id_product and quantity here can be duplicate because this table is larger, containing id_bill and other information)
products table has the columns: id, name
SELECT products.name AS Product,
bill_content.quantity AS Quantity
FROM bill_content, products
WHERE bill_content.id = products.id
ORDER BY bill_content.quantity DESC
LIMIT 10
Of course this returns a table of 2 rows containing all the products and their quantity in the bill_content table, but there are duplicates and I need to make sum of their quantity and display them as a single row.
Thank you in advance.
ANSWERED
This could be done using GROUP BY as Gordon Linoff said.
You want a group by. You should also learn to use proper explicit join syntax:
SELECT p.name AS Product,
SUM(bc.quantity) AS Quantity
FROM bill_content bc JOIN
products p
ON bc.id = p.id
GROUP BY p.name
ORDER BY SUM(bc.quantity) DESC
LIMIT 10;

MYSQL Count group by rows ignoring effect of JOIN and SUM fields on Joined tables

I have 3 tables:
Orders
- id
- customer_id
Details
- id
- order_id
- product_id
- ordered_qty
Parcels
- id
- detail_id
- batch_code
- picked_qty
Orders have multiple Details rows, a detail row per product.
A detail row has multiple parcels, as 10'000 ordered qty may come from 6 different batches, so goods from batches are packed and shipped separately. The picked quantity put in each parcel for a detail row should then be the same as the ordered_qty.
... hope that makes sense.
Im struggling to write a query to provide summary information of all of this.
I need to Group By customer_id to provide a row of data per customer.
That row should contain
Their total number of orders
Their total ordered_qty of goods across all orders
Their total picked_qty of goods across all orders
I can get the first one with:
SELECT customer_id, COUNT(*) as number_of_orders
FROM Orders
GROUP BY Orders.customer_id
But when I LEFT JOIN the other two tables and add the
SELECT ..... SUM(Details.ordered_qty) AS total_qty_ordered,
SUM(Parcels.picked_qty) AS total_qty_picked
.. then I get results that dont seem to add up for the quantities, and the COUNT(*) seems to include the additional lines from the JOIN which obviously then isn't giving me the number of Orders anymore.
Not sure what to try next.
===== EDIT =======
Here's the query I tried:
SELECT
customer_id,
COUNT(*) as number_of_orders,
SUM(Details.ordered_qty) AS total_qty_ordered,
SUM(Parcels.picked_qty) AS total_qty_picked
FROM Orders
LEFT JOIN Details ON Details.order_id=Order.id
LEFT JOIN Parcels ON Parcels.detail_id=Detail.id
GROUP BY Orders.customer_id
try COUNT(distinct Orders.order_id) as number_of_orders,
as in
SELECT
customer_id,
COUNT(distinct Orders.order_id) as number_of_orders,
SUM(Details.ordered_qty) AS total_qty_ordered,
(select SUM(Parcels.picked_qty)
FROM Parcels WHERE Parcels.detail_id=Detail.id ) AS total_qty_picked
FROM Orders
LEFT JOIN Details ON Details.order_id=Order.id
GROUP BY Orders.customer_id
EDIT: added an other select with subselect
Is there any particular reason you feel the need to combine all these in one query? Simplify by breaking it up in to separate queries, and if you want a single call to get the results, put the queries in a stored procedure, using temp tables.