交易表
No Date Amount
1 06-07-2017 1000
2 06-07-2017 1500
3 08-07-2017 2000
4 09-07-2017 2000
5 09-07-2017 2000
6 09-07-2017 2000
是否可以通过单个查询(无查询循环)实现此结果
No Date Total Amount
1 06-07-2017 2500
2 08-07-2017 2000
3 09-07-2017 6000
答案 0 :(得分:1)
您在寻找Group By
吗?
select Trunc("Date"), -- we have to put ".." since Date is a Keyword in Oracle
sum(Amount) as "Total Amount"
from MyTable
group by Trunc("Date")
order by Trunc("Date")
修改:似乎Date
字段包含日期和时间,而时间部分应该截断< / em> - Trunc
- 汇总时(参见评论)
答案 1 :(得分:1)
确切结果:
select row_number() over (order by date) as No,
date, sum(amount) as "Total Amount"
from t
group by date
order by date;
注意:在Oracle中,date
数据类型可以包含时间组件 - 这可能在输出中不可见。如果是这样,聚合不会按预期执行。如果是这种情况,那么:
select row_number() over (order by trunc(date)) as No,
trunc(date) as date, sum(amount) as "Total Amount"
from t
group by trunc(date)
order by trunc(date);