同时Max价格的数量总和

时间:2017-11-06 21:15:29

标签: sql oracle sum max aggregate-functions

表格数据:

Time    BUY  SELL   BUY_Qty SELL_Qty
3:00    20     10      5     2
3:00    20     10      5     1
3:00    20     10      5     4
3:00    10     20      2     3
3:00    10     20      2     3

以上数据的预期结果:

Time    max(BUY)  MIN(SELL)   BUY_Qty SELL_Qty
3:00       20          10       15      7

1 个答案:

答案 0 :(得分:0)

以下是您可以在oracle中使用的查询:

select t1.Time, max_buy, min_sell, SUM(t1.BUY_Qty), SUM(t1.SELL_Qty) 
from table1 t1
join(SELECT Time, MAX(BUY) as max_buy, MIN(SELL) as min_sell
    FROM Table1 GROUP BY Time) t2 
    on t1.time=t2.time and t1.buy=t2.max_buy
group by t1.Time, max_buy, min_sell

SQL DEMO HERE