连接表中的总和字段

时间:2017-06-16 20:23:30

标签: sql database ms-access

查询订单表:

SELECT O.ID, C.SecondName, E.SecondName, O.DateOf , O.ClientID, O.EmployeeID, O.Desc
FROM ((Client AS C INNER JOIN [ORDER] AS O ON C.ID = O.ClientID) 
    INNER JOIN Employee AS E ON E.ID = O.EmployeeID) 

查询订单商品表:

SELECT  OrderItem.ID, 
        ProductID, 
        OrderID, 
        Quantity, 
        P.Title,
        P.CurrentPrice, 
        P.ID, 
        (P.CurrentPrice* OrderItem.Quantity) AS Total
FROM 
    OrderItem 
INNER JOIN 
    Product AS P 
ON 
    OrderItem.ProductID = P.ID
GROUP BY 
    OrderID,
    OrderItem.ID, 
    ProductID, 
    Quantity, 
    P.Title,
    P.CurrentPrice, 
    P.ID

和总订单价格:

select OrderID, sum(Total)
from (
SELECT 
    OrderItem.ID
    , ProductID
    , OrderID
    , Quantity
    , P.Title
    ,P.CurrentPrice
    , P.ID
    , (P.CurrentPrice* OrderItem.Quantity) AS Total
FROM OrderItem 
INNER JOIN Product AS P ON OrderItem.ProductID = P.ID
) t 

group by OrderId

如何连接表以获取订单表的总订单价格字段?

2 个答案:

答案 0 :(得分:0)

如果你需要接下来每一行thw sum_total你可以这样加入

(否则你应该使用在orderID上循环合并两个查询的表示层)

select 
    t1.ID
  , t1.ProductID
  , t1.OrderID
  , t1.uantity
  , t1.Title
  , t1.CurrentPrice
  , t1.PID
  , t1.Total
  , T2.sum_total
FROM( 
  SELECT  OrderItem.ID, 
          ProductID, 
          OrderID, 
          Quantity, 
          P.Title,
          P.CurrentPrice, 
          P.ID  AS PID, 
          (P.CurrentPrice* OrderItem.Quantity) AS Total
  FROM 
      OrderItem 
  INNER JOIN 
      Product AS P 
  ON 
      OrderItem.ProductID = P.ID
  GROUP BY 
      OrderID,
      OrderItem.ID, 
      ProductID, 
      Quantity, 
      P.Title,
      P.CurrentPrice, 
      P.ID
  ) t1 
INNER JOIN(
    select OrderID, sum(Total) sum_total
    from (
    SELECT 
        OrderItem.ID
        , ProductID
        , OrderID
        , Quantity
        , P.Title
        ,P.CurrentPrice
        , P.ID
        , (P.CurrentPrice* OrderItem.Quantity) AS Total
    FROM OrderItem 
    INNER JOIN Product AS P ON OrderItem.ProductID = P.ID
    ) t 
    group by OrderId
) t2 on t1.OrderID = t2.OrderID

答案 1 :(得分:0)

1)第三个查询,即所谓的“总订单价格”,有一个不必要的子查询和字段。这是等效的:

SELECT 
    OrderID, 
    Sum(P.CurrentPrice* OrderItem.Quantity) AS OrderTotal
FROM OrderItem INNER JOIN Product AS P ON OrderItem.ProductID = P.ID
GROUP BY OrderId

2)我将您对“订单表的订单价格字段”的请求解释为您希望获得订单详细信息以及订单价格。(?)您已经在第一个查询中演示了正确的多表连接。只需遵循相同的模式即可加入[Order]表...以及可选的客户和/或员工表。

只有订单表:

SELECT  
    O.ID, O.DateOf , O.ClientID, O.EmployeeID, O.Desc,
    Sum(P.CurrentPrice* OrderItem.Quantity) AS [OrderTotal]
FROM
    ([Order] As O INNER JOIN OrderItem ON O.ID = OrderItem.OrderID)
    INNER JOIN Product AS P ON OrderItem.ProductID = P.ID
GROUP BY 
    O.ID, O.DateOf , O.ClientID, O.EmployeeID, O.Desc

关键想法是任何不在sum()或其他聚合函数中的选定字段必须在Group By子句中。

SELECT  
    O.ID, C.SecondName, E.SecondName, 
    O.DateOf , O.ClientID, O.EmployeeID, O.Desc,
    Sum(P.CurrentPrice* OrderItem.Quantity) AS [OrderTotal]
FROM ((
    (Client AS C INNER JOIN [ORDER] AS O ON C.ID = O.ClientID)
    INNER JOIN Employee AS E ON E.ID = O.EmployeeID)
    INNER JOIN OrderItem ON O.ID = OrderItem.OrderID)
    INNER JOIN Product AS P ON OrderItem.ProductID = P.ID
GROUP BY 
    O.ID, C.SecondName, E.SecondName, O.DateOf,
    O.ClientID, O.EmployeeID, O.Desc