简体   繁体   English

SQL计数不适用于多个表

[英]SQL Count Not working over multiple tables

I am teaching myself SQL and have been using the W3Schools website. 我正在自学SQL,并且一直在使用W3Schools网站。 On their website they have a dummy database that they use to demonstrate different things. 在他们的网站上,他们有一个虚拟数据库,用于演示不同的事物。 You can also use this to try things out your self. 您也可以使用它来尝试自己的事情。 Their TryIt editor can be found at https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_distinct 可以在https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_distinct上找到其TryIt编辑器。

After working through their examples I have been inventing questions and trying to answer them. 在研究了它们的示例之后,我一直在发明问题并尝试回答它们。 However, I have come across something that does not appear to be working and I wanted to know if it was an error in my code, error on the W3Schools site or just a limitation of SQL. 但是,我遇到了一些似乎无法正常工作的事情,我想知道这是我的代码错误,W3Schools网站上的错误还是SQL的限制。

When I run the following code it works as expected and returns the number of orders for each customer. 当我运行以下代码时,它可以按预期工作,并返回每个客户的订单数。

SELECT c.CustomerName, COUNT(o.OrderID) AS NumberOfOrders
FROM Customers AS C
LEFT JOIN Orders AS o ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerName

I then decided to try and extend the query to also give the total amount of those orders using the following code. 然后,我决定尝试使用以下代码扩展查询,以给出这些订单的总额。

SELECT c.CustomerName, COUNT(o.OrderID) AS NumberOfOrders, SUM(od.Quantity * p.price) AS TotalOfOrders
FROM (((Customers AS C
LEFT JOIN Orders AS o ON c.CustomerID = o.CustomerID)
LEFT JOIN OrderDetails AS od ON o.OrderID = od.OrderID)
LEFT JOIN Products AS p ON od.ProductID = p.ProductID)

Now when I run the code count on the OrderID is not working. 现在,当我运行代码时,OrderID不能正常工作。 For example for customer Ana Trujillo Emparedados y helados (CustomerID 2) it returns 2 instead of 1. This is the number of items that their one order contained. 例如,对于客户Ana Trujillo Emparedados y helados(客户ID 2),它返回2而不是1。这是他们的一个订单包含的项目数。

Am I missing something obvious? 我是否缺少明显的东西?

First of all, in your second query the group by clause is missing. 首先,在第二个查询中,缺少group by子句。 You are doing an error called "double counting". 您正在执行一个错误,称为“重复计数”。 For the case you are naming (Trujillo Emparedados y helados), the count is right because there are 2 details related the only order related to the customer. 对于您要命名的情况(Trujillo Emparedados y helados),计数是正确的,因为有2个详细信息与唯一与客户相关的订单相关。 So now you are not counting the orders, but you are counting the order details. 因此,现在您不计算订单,而是计算订单明细。 To obtain the "1" you want you have to fix the query with a "distinct" in this way: 要获得“ 1”,您必须以这种方式用“ distinct”修复查询:

SELECT c.CustomerName, COUNT(distinct o.OrderID) AS NumberOfOrders, 
SUM(od.Quantity * p.price) AS TotalOfOrders
FROM (((Customers AS C
LEFT JOIN Orders AS o ON c.CustomerID = o.CustomerID)
LEFT JOIN OrderDetails AS od ON o.OrderID = od.OrderID)
LEFT JOIN Products AS p ON od.ProductID = p.ProductID)
group by c.CustomerName

There is nothing wrong with your query and result, 您的查询和结果没有错,

It is because OrderDetails.ProductID 69 and 70 in same orderID (10308) 这是因为OrderDetails.ProductID 69和70具有相同的orderID(10308)

You can get full result with select * : 您可以通过select *获得完整结果:

SELECT *
FROM (((Customers AS C
LEFT JOIN Orders AS o ON c.CustomerID = o.CustomerID)
LEFT JOIN OrderDetails AS od ON o.OrderID = od.OrderID)
LEFT JOIN Products AS p ON od.ProductID = p.ProductID)
WHERE c.CustomerID = 2

在w3school中,您可以使用有限的加入条件,所以请使用您的私有服务器:)

Seems to work when I amend the SELECT statement to 当我将SELECT语句修改为

SELECT c.CustomerName, COUNT (DISTINCT o.OrderID) AS NumberOfOrders, SUM(od.Quantity * p.Price) AS TotalOfOrders

Thanks Colour Dalnet for your suggestion. 感谢Color Dalnet的建议。

Have a look at this statement 看看这句话

SELECT c.CustomerID,c.CustomerName, o.OrderId
FROM (((Customers AS C
LEFT JOIN Orders AS o ON c.CustomerID = o.CustomerID)
LEFT JOIN OrderDetails AS od ON o.OrderID = od.OrderID)
 )

this shows you that your left join shows the orderId twice. 这表明您左联接两次显示orderId。 And the count operator counts it twice. 计数操作员将其计数两次。

You need to count distinct values 您需要计算不同的值

This should be correct: 这应该是正确的:

SELECT c.CustomerID,c.CustomerName, COUNT(DISTINCT o.OrderID) AS NumberOfOrders, SUM(od.Quantity * p.price) AS TotalOfOrders
FROM (((Customers AS C
LEFT JOIN Orders AS o ON c.CustomerID = o.CustomerID)
LEFT JOIN OrderDetails AS od ON o.OrderID = od.OrderID)
LEFT JOIN Products AS p ON od.ProductID = p.ProductID)
GROUP by c.CustomerID,c.CustomerName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM