对于给定的日期范围,Oracle按周累计总计

时间:2015-06-29 16:05:18

标签: oracle10g

我使用以下查询来获取给定日期范围的每周列总数...

SELECT to_char(week_start - 1, 'dd-MON-yy') week_end, run_qty, acc_qty, CASE WHEN run_qty <> 0 THEN ROUND(acc_qty/run_qty, 4) ELSE 0 END pct

  FROM (SELECT week_start, SUM(run_qty) run_qty, SUM(acc_qty) acc_qty

          FROM (SELECT TRUNC(NEXT_DAY(TRUNC(created_date), 'Monday')) week_start, NVL(SUM(run_qty), 0) run_qty, NVL(SUM(accepted_qty), 0) acc_qty

                  FROM shema.table_a 
                  WHERE (some conditions)
                    AND created_date BETWEEN :FromDate AND :ToDate 
                  GROUP BY TRUNC(NEXT_DAY(TRUNC(created_date), 'Monday'))
                 UNION
                 SELECT TRUNC(NEXT_DAY(TRUNC(to_date(zday, 'dd-mon-rrrr')), 'Monday')) week_start, 0run_qty, 0acc_qty
                   FROM (SELECT :FromDate + (level - 1) zday
                           FROM dual
                         CONNECT BY LEVEL <= (:ToDate - :FromDate)))
          GROUP BY week_start
          ORDER BY week_start desc)

输入参数:FromDate = 4/31/2015 and:ToDate = 6/25/2015,这给了我每周的最后一天(周定义为周一至周日),每周的运行总数,每周接受的总数,以及每周接受的运行总数的百分比,在结果集中看起来如此...

28-JUN-2015 0 0 0

21-JUN-2015 100 50 0.5

14-JUN-2015 50 40 0.8

07-JUN-2015 0 0 0

31-MAY-2015 0 0 0

24-MAY-2015 50 40 0.75

17-MAY-2015 80 50 0.625

10-MAY-2015 60 20 0.3333

03-MAY-2015 0 0 0

我是否可以使用类似的方法来计算运行的总运行次数和接受的数量以及在提供的日期范围内接受的数量百分比? (给我一个看起来像的结果集)......

28-JUN-2015 340 200 0.5882

21-JUN-2015 340 200 0.5882

14-JUN-2015 240 150 0.625

07-JUN-2015 190 110 0.5789

31-MAY-2015 190 110 0.5789

24-MAY-2015 190 110 0.5789

17-MAY-2015 140 70 0.5

10-MAY-2015 60 20 0.3333

03-MAY-2015 0 0 0

1 个答案:

答案 0 :(得分:0)

我想通了......如果有人通过搜索类似问题的解决方案遇到此线程,我将上面的查询包装在内... 选择week_end,run_qty,acc_qty,pct

FROM(上面的查询)
模型    DIMENSION BY(row_number()OVER(ORDER BY to_date(week_end,'dd-MON-yy')asc)rec)

MEASURES(week_end,run_qty,acc_qty,pct)

RULES(

run_qty [rec&gt; 1] ORDER BY rec = run_qty [cv()] + run_qty [cv() - 1],

acc_qty [rec&gt; 1] ORDER BY rec = acc_qty [cv()] + acc_qty [cv() - 1],

pct [rec&gt; = 0] ORDER BY rec = CASE WHEN run_qty [cv()]&lt;&gt; 0 THEN ROUND(acc_qty [cv()] / run_qty [cv()],4)ELSE 0 END           )

ORDER BY to_date(week_end,'dd-MON-yy')desc
......它运行得很快,给我预期的结果