tags:

views:

116

answers:

1

Can anybody let us know an easier way to get the result as shown by following query? We need to find a way that can show us the same result as the following query:

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)

The output is like:

    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  A: 

Hi, I'd change the part

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

to

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

as it may be faster. But I've no idea to circumvent the DECODE stuff.

tuergeist