我需要两个表来创建查询。我相信我也需要加入他们。
我需要从CustomerName
表中获取客户的Address
和Customer
,但我还需要查询以显示客户下达的所有订单的总金额。因此,如果客户今年总共花费了300美元,那么300美元就是我想要实现的产出。
我有一个名为Order Details
的表,其使用的OrderID
与CustomerID
绑定,unitprice
和quantity
列位于Order Details
1}}表。我正在试图弄清楚如何复制这些但我会发疯。
我试过这个以至少从订单中得到总数,但我确实有语法错误:
SELECT unitprice,
quantity
FROM [Order details] (unitprice * quantity) AS Totalorders,
from [Order Details]
WHERE orderid > 0
这也没有运气:
SELECT customers.companyname AS 'Company Name',
customers.address AS 'Address',
[order details].unitprice * [order details].quantity AS 'Orders'
FROM customers
LEFT JOIN orders
ON customers.customerid = orders.customerid
ORDER BY customers.companyname,
customers.address,
orders
由于
答案 0 :(得分:0)
如果它是一对多的关系,您的查询将类似于:
SELECT
Customers.CompanyName AS 'Company Name',
Customers.Address AS 'Address',
SUM(O.Unitprice * O.quantity) as 'Orders'
FROM Customers
LEFT JOIN [Order Details] O
ON Customers.CustomerID = O.CustomerID
GROUP BY Customers.CompanyName, Customers.Address
ORDER BY Customers.CompanyName, Customers.Address
答案 1 :(得分:0)
您需要加入订单详细信息并通过汇总进行分组:
SELECT c.CompanyName AS "Company Name",
c.Address AS "Address",
sum(od.Unitprice * od.quantity) as "Orders"
FROM Customers c LEFT JOIN
Orders o
ON c.CustomerID = o.CustomerID left join
[Order Details] od
on od.orderid = o.orderid
GROUP BY c.CompanyName, c.Address
ORDER BY c.CompanyName, c.Address
此外,我做了一些风格上的改变。我在表名中添加了别名。我发现阅读c.CompanyName
而不是Customers.CompanyName
要容易得多。我更改了列别名上的分隔符以使用双引号而不是单引号。我将单引号与语句中的字符串常量相关联。