使用sql查询获取订单总数

时间:2016-02-25 23:24:17

标签: sql sql-server sql-server-2008

嗨,我想说我有以下两个表格

第一个表格是Orders,其中包含以下列

Order_ID  OrderDate
1          2016-01-20
2          2016-01-21

,第二个表是OrderDetails,其中包含以下列

Order_ID ProductID UnitPrice Quantity
1         1        5.00       1
1         3        20.00      3
2         2        10.00      2

我如何得到以下输出

Order_ID  OrderDate    OrderTotalamount
 1        2016-01-20    65.00
 2        2016-01-21    20.00

3 个答案:

答案 0 :(得分:2)

你介意尝试这样的东西吗?

SELECT ORD.Order_ID, ORD.OrderDate, SUM(ORDD.UnitPrice * ORDD.Quanity) AS OrderTotalAmount

FROM Orders ORD

INNER JOIN OrderDetails ORDD
ON ORDD.Order_ID = ORD.Order_ID

GROUP BY ORD.Order_ID, ORD.OrderDate

答案 1 :(得分:2)

你可以试试这个

SELECT a.Order_ID, a.OrderDate, SUM(Quantity*UnitPrice) as OrderTotalamout
  FROM Orders a JOIN OrderDetains ON (a.Order_ID = OrderDetails.Order_ID) 
 GROUP BY 1, 2; 

我希望它适合你。

答案 2 :(得分:1)

您可以使用

SELECT TABLE1.Order_ID, TABLE1.OrderDate, SUM(TABLE2.Quantity*TABLE2.UnitPrice) as TotalPrice
  FROM TABLE1 JOIN TABLE2 WHERE(TABLE1.Order_ID = TABLE2.Order_ID);