我是Northwind示例数据库。它有8个表(Customers
,Categories
,Employees
,OrderDetails
,Orders
,Products
,Shipper
和{{1我应用查询来计算每个客户的总订单数量和他迄今为止所花的钱。查询在这里:
Suppliers
但它没有正确计算totalOrders。我从4个表select c.CustomerID,
SUM(p.Price*od.Quantity) as Totalspent,
COUNT(o.OrderID) AS TotalOrder
from Orders o LEFT join Customers c ON c.CustomerID = o.CustomerID
join [OrderDetails] od on od.OrderID = o.OrderID
join Products p on od.ProductID = p.ProductID
group by c.CustomerID order by c.customerid
,Orders
,Customers
和OrderDetails
中提取数据。检查表的以下链接。
http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all