0

我有下表(test_table):

date   value
---------------
d1      10.0
d1      20.0
d2      60.0
d2      10.0
d2     -20.0
d3      40.0

我计算运行总数如下。我两次使用相同的查询,因为首先我需要计算特定日期的值,然后我可以计算运行总计。否则,加入两个日期不是唯一的表,每个日期都会得到太多结果:

  SELECT t1.date, SUM(t2.value) AS total

    FROM

 (SELECT date, SUM(value) AS value FROM test_table GROUP BY date) AS t1

    JOIN

 (SELECT date, SUM(value) AS value FROM test_table GROUP BY date) AS t2

      ON t1.date >= t2.date
GROUP BY t1.date
ORDER BY t1.date

这给了我(很好):

date    total
-------------
d1       30.0
d2       80.0
d3      120.0

但是,这个查询效率不高,因为如果需要,我需要在两个地方更改条件。在生产中,test_table 要大得多(> 4 Mio. rows),并且查询需要太多时间才能完成。问题:如何避免两次使用相同的查询?

4

1 回答 1

0

@Kiril,检查以下是否适合您:

SELECT a1.date, a1.value, SUM(a2.value) Running_Total
FROM test_table a1, test_table a2
WHERE a1.date >= a2.date
GROUP BY a1.date, a1.value
ORDER BY a1.date, a1.value;

o/p:
date          value    running_total
01/01/2014     10               30
01/01/2014     20               30
01/11/2014    -20               80
01/11/2014     10               80
01/11/2014     60               80
01/15/2014     40              120

如果您需要进一步细化,可以将此数据加载到临时表中,然后:

select distinct date , running_total from tem_table;

另一种简单的两步法:

Step1: (This will give you distinct dates in column 1)
create temp table tt (val_date date, val_value smallint);
insert into tt (val_date, val_date)
select date, sum(value) from test_table group by date;

Step2:
SELECT a1.val_date, SUM(a2.val_value) Running_Total
FROM tt a1, tt a2
WHERE a1.val_date >= a2.val_date
GROUP BY a1.val_date;

注意:这两种方法都经过尝试和测试。

谢谢和欢呼,

快乐优化。

于 2014-09-05T09:44:49.120 回答