具有多个视图的存储过程

时间:2015-08-23 13:00:21

标签: sql

我可以为以下情况创建存储过程吗?如果不是最好的方法是什么?

我从下表开始:

ccrcode   accountno
-------------------
1          100
1          110
1          120
2          100
2          200
2          210
3          100
3          300
3          320
4          400
4          410
4          420
5          500
5          510
5          520
6          410
6          600
6          620
7          700
7          710
7          720
8          800
8          810
8          720
9          900
9          910
9          920
10         1000
10         1010
10         1020

我正在运行以下查询:

create view step1
as
   select 
       A.ccrcode, A.accountno, 
       dense_rank() over (order by accountno) as V1
   from 
       all$ A

create view step2
as
   select 
       A.ccrcode, A.accountno, A.V1, B.V2
   from 
       step1 A
   join 
       (select ccrcode, min(V1) V2 
        from step1 
        group by ccrcode) B on B.ccrcode = A.ccrcode

create view step3
as
   select 
       A.ccrcode, A.accountno, A.V2 , B.V3
   from step2 A
   join (select accountno, min(V2) V3 
         from step2  
         group by accountno) B on B.accountno = A.accountno

create view step4
as
   select 
       A.ccrcode, A.accountno, A.V3, B.V4
   from step3 A
   join (select ccrcode, min(V3) V4 
         from step3 
         group by ccrcode) B on B.ccrcode = A.ccrcode

我想在step4 table whiteout中生成结果,创建所有其他表。

1 个答案:

答案 0 :(得分:2)

您不需要观看。只需使用子查询或CTE:

with step1 as (
      select A.ccrcode, A.accountno, dense_rank() over (order by accountno) as V1
      from all$ A
     ),
     step2 as
     (select A.ccrcode, A.accountno, A.V1, B.V2
      from step1 A join
           (select ccrcode, min(V1) V2 from step1 group by ccrcode) B
           on B.ccrcode = A.ccrcode
     ),
     step3 as
     (select A.ccrcode, A.accountno, A.V2 , B.V3
      from step2 A join
           (select accountno, min(V2) V3 from step2 group by accountno) B
           on B.accountno = A.accountno
     ),
     step4 as
     (select A.ccrcode, A.accountno, A.V3, B.V4
      from step3 A join
           (select ccrcode, min(V3) V4 from step3 group by ccrcode) B
           on B.ccrcode = A.ccrcode
     )
select *
from step4;