3

假设以下情况:

  • 第 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)来做到这一点。

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

4

3 回答 3

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                                                                    
于 2012-09-12T04:57:27.320 回答
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

于 2012-09-12T04:51:34.490 回答
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)
于 2012-09-12T03:42:48.413 回答