使用sql查询获取订单总数
嗨,假设我有以下两张桌子 第一个表是订单,由以下列组成使用sql查询获取订单总数,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,嗨,假设我有以下两张桌子 第一个表是订单,由以下列组成 Order_ID OrderDate 1 2016-01-20 2 2016-01-21 Order_ID ProductID UnitPrice Quantity 1 1 5.00 1 1 3 20.00 3 2 2 10.00 2 第二个表是OrderDetails,由以下列
Order_ID OrderDate
1 2016-01-20
2 2016-01-21
Order_ID ProductID UnitPrice Quantity
1 1 5.00 1
1 3 20.00 3
2 2 10.00 2
第二个表是OrderDetails
,由以下列组成
Order_ID OrderDate
1 2016-01-20
2 2016-01-21
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
你介意试试这样的吗
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
你介意试试这样的吗
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
你可以试试这个
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;
我希望它对你有用 你可以试试这个
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;
我希望它对你有用 您可以使用
SELECT TABLE1.Order_ID, TABLE1.OrderDate, SUM(TABLE2.Quantity*TABLE2.UnitPrice) as TotalPrice
FROM TABLE1 JOIN TABLE2 WHERE(TABLE1.Order_ID = TABLE2.Order_ID);
你可以用
SELECT TABLE1.Order_ID, TABLE1.OrderDate, SUM(TABLE2.Quantity*TABLE2.UnitPrice) as TotalPrice
FROM TABLE1 JOIN TABLE2 WHERE(TABLE1.Order_ID = TABLE2.Order_ID);
太快了!为了我自己的启发@fmgonzalez,陈述结尾的1,2是什么?我猜它表示要选择的第一项和第二项上的组?我更喜欢使用列名,因为将来您可能会在select语句中更改列的顺序。是的,您猜得很好。我已经习惯了。抱歉,如果它看起来晦涩难懂。@sqluser我同意你的看法,即使为了便于阅读,最好添加列名。不管怎样,我只在像这样的短查询中使用这种表示法,或者在select中只有几个字段,或者列名太长。太快了!为了我自己的启发@fmgonzalez,陈述结尾的1,2是什么?我猜它表示要选择的第一项和第二项上的组?我更喜欢使用列名,因为将来您可能会在select语句中更改列的顺序。是的,您猜得很好。我已经习惯了。抱歉,如果它看起来晦涩难懂。@sqluser我同意你的看法,即使为了便于阅读,最好添加列名。无论如何,我只在像这样的短查询中使用这种表示法,或者在select中只有几个字段,或者列名太长时使用这种表示法。