答案 0 :(得分:3)
使用分析函数仅使用一次表扫描执行查询:
Oracle 11g R2架构设置:
CREATE TABLE your_table( today, tot_hits ) As
SELECT DATE '2018-01-16', 5498 FROM DUAL UNION ALL
SELECT DATE '2018-01-17', 4235 FROM DUAL;
查询1 :
SELECT t.*,
SUM( tot_hits ) OVER ( ORDER BY today ) AS tot_hits_to_date
FROM your_table t
<强> Results 强>:
| TODAY | TOT_HITS | TOT_HITS_TO_DATE |
|----------------------|----------|------------------|
| 2018-01-16T00:00:00Z | 5498 | 5498 |
| 2018-01-17T00:00:00Z | 4235 | 9733 |
答案 1 :(得分:1)
试试这个
SELECT
Today,
Hits,
TillDate = Hits+NVL((SELECT SUM(Hits) FROM YourTable WHERE Today < T.Today),0)
FROM YourTable T