更简单的方法来获得结果,如下面的sql查询所示(oracle数据库)

时间:2009-08-31 11:02:03

标签: sql oracle plsql

任何人都可以告诉我们更简单的方法来获得结果,如下面的查询所示?我们需要找到一种方法,可以向我们显示与以下查询相同的结果:

SELECT  DISTINCT A.bio_id ,
    DECODE(A.wk_units2 - B.wk_units1,0,NULL,A.wk_units2) prev,
    DECODE(A.wk_units2 - B.wk_units1,0,NULL,B.wk_units1) curr, 
    DECODE(A.wk_units3 -B.wk_units2,0,NULL,A.wk_units3) prev, 
    DECODE(A.wk_units3 -B.wk_units2,0,NULL,B.wk_units2) curr, 
    DECODE(A.wk_units4 -B.wk_units3,0,NULL,A.wk_units4) prev, 
    DECODE(A.wk_units4 -B.wk_units3,0,NULL,B.wk_units3) curr,
    DECODE(A.wk_units5 -B.wk_units4,0,NULL,A.wk_units5) prev, 
    DECODE(A.wk_units5 -B.wk_units4,0,NULL,B.wk_units4) curr, 
    DECODE(A.wk_units6 -B.wk_units5,0,NULL,A.wk_units6) prev, 
    DECODE(A.wk_units6 -B.wk_units5,0,NULL,B.wk_units5) curr 
     .                                            .
     .                                            .
     .                                            .
   DECODE(A.wk_units105 -B.wk_units104,0,NULL,A.wk_units105) prev, 
   DECODE(A.wk_units105 -B.wk_units105,0,NULL,B.wk_units104) curr 



    FROM 
    cnt_sls_dm.fct_sales_summary A,
    cnt_sls_dm.fct_sales_summary B 
    WHERE 
    A.bio_id=B.bio_id AND A.bio_id<>0 
    AND ROWNUM<25 
    AND ( A.wk_units2<> B.wk_units1 
    OR  A.wk_units3<> B.wk_units2 
    OR  A.wk_units4<> B.wk_units3 
    OR  A.wk_units5<> B.wk_units4 
    OR  A.wk_units6<> B.wk_units5
     .                        .
     .                        .
     .                        .
    OR  A.wk_units105<>B.wk_units104)

输出如下:

    BIO_ID  PREV    CURR    PREV_1  CURR_1  PREV_2  CURR_2  PREV_3  CURR_3
5,032,130.00    -1  0   0   1           0   1
5,032,130.00    1   0   0   1   1   0   0   1
5,032,130.00            0   1           0   1
5,032,130.00            10  1   10  0   0   1
5,032,328.00    53  0   36  0   20  0       
5,032,328.00                            1   0
5,175,147.00    2   0                       
5,175,147.00            2   0               
5,175,147.00                                
21,073,129.00   17  0                   2   0
21,073,129.00   18  0   6   0   2   0       
21,073,129.00                           1   0
21,073,129.00                           3   0
21,073,129.00

1 个答案:

答案 0 :(得分:1)

我要改变部分

AND ( A.wk_units2<> B.wk_units1 
OR  A.wk_units3<> B.wk_units2 
OR  A.wk_units4<> B.wk_units3  ...

AND NOT ( A.wk_units2 == B.wk_units1 
OR A.wk_units3== B.wk_units2 
...

因为它可能会更快。但我不知道要绕过DECODE的东西。