[英]Inner Join for 3 tables with SUM of two columns in SQL Query?
I have the following three tables:我有以下三个表:
I have Following Query to Join Above 3 Tables我有以下查询要加入以上 3 个表
customer.customer_id,
customer.name,
SUM(sales.total),
sales.created_at,
SUM(sales_payments.amount)
FROM
sales INNER JOIN customer ON customer.customer_id = sales.customer_id
INNER JOIN sales_payments ON sales.customer_id = sales_payments.customer_id
WHERE sales.created_at ='2020-04-03'
GROUP By customer.name
Result for Above Query is given below上述查询的结果如下
Sum of sales.total
is double of the actual sum of sales.total column which has 2-row count, I need to have the actual SUM of that column, without doubling the SUM of those rows, Thank you, for your help in advance.. sales.total
的总和是具有 2 行计数的 sales.total 列的实际总和的两倍,我需要该列的实际总和,而不是将这些行的总和加倍,谢谢您的帮助..
PROBLEM问题
The problem here is that there are consecutive inner joins and the number of rows getting fetched in the second inner join is not restricted.这里的问题是存在连续的内连接,并且在第二个内连接中获取的行数不受限制。 So, as we have not added a condition on sales_payment_id in the join between the sales and sales_payment tables, one row in sales table(for customer_id 2, in this case) would be mapped to 2 rows in the payment table.
因此,由于我们没有在 sales 和 sales_payment 表之间的连接中添加 sales_payment_id 条件,因此 sales 表中的一行(在本例中为 customer_id 2)将映射到 payment 表中的 2 行。 This causes the same values to be reconsidered.
这会导致重新考虑相同的值。 In other words, the mapping for customer_id 2 between the 3 tables is 1:1:2 rather than 1:1:1.
也就是说,customer_id 2 在 3 个表之间的映射是 1:1:2 而不是 1:1:1。
SOLUTION解决方案
Solution 1: As mentioned by Gordon, you could first aggregate the amount values of the sales_payments table and then aggregate the values in sales table.解决方案 1:正如 Gordon 所说,您可以先聚合 sales_payments 表的金额值,然后聚合 sales 表中的值。
Solution 2: Alternatively (IMHO a better approach), you could add a foreign key between sales and sales_payment tables.解决方案 2:或者(恕我直言,一种更好的方法),您可以在 sales 和 sales_payment 表之间添加一个外键。 For example, the sales_payment_id column of sales_payment table can be introduced in the sales table as well.
例如,sales_payment 表的sales_payment_id 列也可以引入sales 表中。 This would facilitate the join between these tables and reduce additional overheads while querying data.
这将促进这些表之间的连接,并减少查询数据时的额外开销。
The query would then look like:查询将如下所示:
`SELECT c.customer_id,
c.name,
SUM(s.total),
s.created_at,
SUM(sp.amount)
FROM customer c
INNER JOIN sales s
ON c.customer_id = s.customer_id
INNER JOIN sales_payments sp
ON c.customer_id = sp.customer_id
AND s.sales_payments_id = sp.sales_payments_id
WHERE s.created_at ='2020-04-03'
GROUP BY c.customer_id,
c.name,
s.created_at ;`
Hope that helps!希望有帮助!
You have multiple rows for sales_payments
and sales
per customer.每个客户的
sales_payments
和sales
有多行。 You need to pre-aggregate to get the right value:您需要预先聚合以获得正确的值:
SELECT c.customer_id, c.name, s.created_at, s.total, sp.amount
FROM customer c JOIN
(SELECT s.customer_id, s.created_at, SUM(s.total) as total
FROM sales s
WHERE s.created_at ='2020-04-03'
GROUP BY s.customer_id, s.created_at
) s
ON c.customer_id = s.customer_id JOIN
(SELECT sp.customer_id, SUM(sp.amount) as amount
FROM sales_payments sp
GROUP BY sp.customer_id
) sp
ON s.customer_id = sp.customer_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.