我想编写一个查询来获取每个地区的总销售额,但是我不知道该怎么做。我有这些表:
Region(RegionID, Name)
Client(ClientID, Name, City, Country, Email, RegionID)
Order(OrderID, Orderdate, ClientID)
Orderdetail(OrderID, ProductID, Quantityordered, Quantitydelivered)
Product(ProductID, Name, UnitPrice)
答案 0 :(得分:0)
首先加入所有表,并求和。 试试这个:
SELECT r.Name AS RegionName, TotalQuantityOrdered = SUM(od.Quantityordered), TotalQuantityDelivered = SUM(Quantitydelivered)
FROM Orderdetail od
INNER JOIN Order o on od.OrderID = o.OrderID
Inner join Client c on o.ClientID = c.ClientID
Inner Join Region r on c.RegionId = r.RegionId
Group By r.Name