如何为每个区域的总销售额创建选择

时间:2019-05-14 17:52:11

标签: sql sql-server select sql-server-2012

我想编写一个查询来获取每个地区的总销售额,但是我不知道该怎么做。我有这些表:

Region(RegionID, Name)
Client(ClientID, Name, City, Country, Email, RegionID)
Order(OrderID, Orderdate, ClientID)
Orderdetail(OrderID, ProductID, Quantityordered, Quantitydelivered)
Product(ProductID, Name, UnitPrice)

1 个答案:

答案 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