处理连续的行计算

时间:2012-09-12 02:59:54

标签: sql oracle plsql oracle11g

假设以下情况:

  • 第1周:
    • 0 之前的案例
    • 10 新案例
    • 3 已解决的案例
  • 第2周:
    • 7 之前的案例
    • 13 新案例
    • 15 已解决的案件
  • 第3周:
    • 5 之前的案例
    • 6 新案例
    • 7 已解决的案件

此信息存储在以下类别的恢复表中:

RESUME_TABLE:
WEEK    | TOTAL_NEW |   TOTAL_SOLVED
1       |   10      |   3
2       |   13      |   15
3       |   6       |   7

我很难构建查询以获得以下结果:

REPORT_TABLE:
WEEK    |   PREV_TOTAL  |   NEW_CASES   |   SOLVED_CASES    |   NEW_TOTAL
1       |   0           |   10          |   3               |   7
2       |   7           |   13          |   15              |   5
3       |   5           |   6           |   7               |   4

这个想法似乎很简单,NEW_TOTAL = PREV_TOTAL + NEW_CASES - SOLVED_CASES,尽管我一直在努力将PREV_TOTAL带到下一行以继续下去。

我正在尝试使用RESUME表(Oracle 11g)上的视图来执行此操作。

有人可以帮我提供一些示例代码吗?

3 个答案:

答案 0 :(得分:3)

分析功能非常简单明了:

12:57:06 HR@vm_xe> l                                                                    
  1  select week                                                                        
  2         ,lag(total_cases_by_now - total_solved_by_now) over (order by week) prev_total
  3         ,total_new new_cases                                                        
  4         ,total_solved solved_cases                                                  
  5         ,total_cases_by_now - total_solved_by_now new_total                         
  6    from (                                                                           
  7    select week                                                                      
  8           ,total_new                                                                
  9           ,total_solved                                                             
 10           ,sum(total_new) over(order by week asc) as total_cases_by_now             
 11           ,sum(total_solved) over (order by week asc) as total_solved_by_now        
 12      from resume_table                                                              
 13* )                                                                                  
12:57:07 HR@vm_xe> /                                                                    

      WEEK   PREV_TOTAL  NEW_CASES SOLVED_CASES  NEW_TOTAL                                
---------- ------------ ---------- ------------ ----------                                
         1                      10            3          7                                
         2            7         13           15          5                                
         3            5          6            7          4                                

3 rows selected.                                                                        

Elapsed: 00:00:00.01                                                                    

答案 1 :(得分:2)

您可以使用MODEL子句解决此问题:

with resume_table as
(
    select 1 week, 10 total_new, 3 total_solved from dual union all
    select 2 week, 13 total_new, 15 total_solved from dual union all
    select 3 week, 6 total_new,  7 total_solved from dual
)
select week, prev_total, total_new, total_solved, new_total
from resume_table
model
    dimension by (week)
    measures (total_new, total_solved, 0 prev_total, 0 new_total)
    rules sequential order
    (
        new_total[any] order by week = 
            nvl(new_total[cv(week)-1], 0) + total_new[cv()] - total_solved[cv()]
        ,prev_total[any] order by week = nvl(new_total[cv(week)-1], 0)
    )
order by week;

虽然这假设WEEK总是一个连续的数字。如果不是这样,您需要添加row_number()。否则,-1可能不会引用之前的值。

请参阅此SQL Fiddle

答案 2 :(得分:0)

RESUME_TABLE中添加一列(或创建一个我认为可能更好的视图):

RESUME_LEFT
WEEK | LEFT
1    | 7
2    | -2
3    | -1

这样的事情:

CREATE VIEW resume_left
  (SELECT week,total_new-total_solved "left" FROM resume_table)

所以在REPORT_TABLE中,您可以得到如下定义:

PREV_TOTAL=(SELECT sum(left) FROM RESUME_LEFT WHERE week<REPORT_TABLE.week)

修改

好的,视图是不必要的:

PREV_TOTAL=(SELECT sum(total_new)-sum(total_solved)
  FROM resume_table
  WHERE week<REPORT_TABLE.week)