SQL在语句中添加

时间:2014-09-18 13:17:19

标签: sql oracle11g

我想返回一堆列并添加其中一列。

订单ID
OrderNum
金额

我希望返回OrderID,金额以及每个orderID金额的总和。像这样的东西(OrderID - OrderNum - Amount - Total):

  • 1 - O1 - $ 4 - $ 10
  • 1 - O1 - $ 3 - $ 10
  • 1 - O1 - $ 3 - $ 10
  • 2 - O2 - $ 4 - $ 8
  • 2 - O2 - $ 3 - $ 8
  • 2 - O2 - $ 1 - $ 8

2 个答案:

答案 0 :(得分:0)

select t1.orderid, t1.amount, t2.sum_amount
from your_table t1
join 
( 
  select orderid, sum(amount) as sum_amount
  from your_table
  group by orderid
) t2 on t1.orderid = t2.orderid

答案 1 :(得分:0)

在Oracle中,您可以使用窗口函数:

SELECT 
    OrderID,
    OrderNum,
    Amount ,
    SUM(Amount) OVER (PARTITION BY OrderID) as Total
FROM OrderLine