Tuning question. Self join vs Analytical function

Hi all,
I am a bit confused about this query cost.
So I have this query. Now follow the original one (after rewritten by me):
SELECT /*+ parallel (d 8) parallel(h 8) parallel(c 8) */
     DISTINCT
d.customer_node_id AS root_customer_node_id,
d.customer_node_id AS customer_node_id,
nvl(h.account_id,c.account_id) AS account_id,
nvl(h.account_name,c.account_name) AS account_name,
d.service_id AS service_id,
nvl((SELECT /*+ parallel(x 8) */ max(x.service_name) FROM delta_service_history x
WHERE x.service_id=d.service_id AND v_upperbound_upd_dt BETWEEN x.effective_start_date AND x.effective_end_date GROUP BY x.service_id),d.service_name) AS service_name
FROM
delta_service_history d,
delta_account c,
stg_hierarchy h
WHERE
d.customer_node_id=c.customer_node_id(+) AND
d.customer_node_id=h.customer_node_id(+)
......the new one (I decided to use analitycal function to calculate max(service_name) for each service_id instead of self join done for "delta_service_history" )
I thought that self join was very heavy....
Anyway, my two questions are:
1. why the second one is heavier than the first. I reduce the number of join.....
2. how can be rewritten the first one query? In particular way I don't like that self join..... :)
Select Distinct
               root_customer_node_id,
               customer_node_id,
               account_id,
               account_name,
               service_id,
service_name
               From
                              SELECT /*+ parallel (d 8) parallel(h 8) parallel(c 8) */
                              d.customer_node_id AS root_customer_node_id,
                              d.customer_node_id AS customer_node_id,
                              nvl(h.account_id,c.account_id) AS account_id,
                              nvl(h.account_name,c.account_name) AS account_name,
                              d.service_id AS service_id,
                              d.service_name,
                              row_number() over (partition by d.service_id order by d.service_name desc) r1
                         FROM
                              delta_service_history d,
                              delta_account c,
                              stg_hierarchy_new h
                         WHERE
                              d.customer_node_id=c.customer_node_id(+) AND
                              d.customer_node_id=h.customer_node_id(+) AND
                              v_upperbound_upd_dt BETWEEN d.effective_start_date AND d.effective_end_date
                         )a
                    Where a.r1 = 1
Thank you all.

I Post query plan.
First one query (the original):
Plan
MERGE STATEMENT ALL_ROWSCost: 2.691.669 Bytes: 784.141.119.324 Cardinality: 1.754.230.692                                                                            
     27 MERGE STGADMIN.STG_HIERARCHY                                                                       
          26 PX COORDINATOR                                                                  
               25 PX SEND QC (RANDOM) PARALLEL_TO_SERIAL SYS.:TQ10005 :Q1005Cost: 2.691.669 Bytes: 475.396.517.532 Cardinality: 1.754.230.692                                                             
                    24 VIEW PARALLEL_COMBINED_WITH_PARENT STGADMIN. :Q1005                                                       
                         23 HASH JOIN RIGHT OUTER BUFFERED PARALLEL_COMBINED_WITH_PARENT :Q1005Cost: 2.691.669 Bytes: 475.396.517.532 Cardinality: 1.754.230.692                                                   
                              4 BUFFER SORT PARALLEL_COMBINED_WITH_CHILD :Q1005                                             
                                   3 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1005Cost: 20,828 Bytes: 860.278.720 Cardinality: 15.362.120                                         
                                        2 PX SEND HASH PARALLEL_FROM_SERIAL SYS.:TQ10000 Cost: 20,828 Bytes: 860.278.720 Cardinality: 15.362.120                                    
                                             1 TABLE ACCESS FULL TABLE STGADMIN.STG_HIERARCHY Cost: 20,828 Bytes: 860.278.720 Cardinality: 15.362.120                               
                              22 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1005Cost: 2.669.426 Bytes: 376.698.378.630 Cardinality: 1.752.085.482                                              
                                   21 PX SEND HASH PARALLEL_TO_PARALLEL SYS.:TQ10004 :Q1004Cost: 2.669.426 Bytes: 376.698.378.630 Cardinality: 1.752.085.482                                         
                                        20 VIEW PARALLEL_COMBINED_WITH_PARENT STGADMIN. :Q1004Cost: 2.669.426 Bytes: 376.698.378.630 Cardinality: 1.752.085.482                                    
                                             19 SORT UNIQUE PARALLEL_COMBINED_WITH_PARENT :Q1004Cost: 2.669.426 Bytes: 127.902.240.186 Cardinality: 1.752.085.482                               
                                                  18 HASH JOIN OUTER PARALLEL_COMBINED_WITH_PARENT :Q1004Cost: 35,386 Bytes: 127.902.240.186 Cardinality: 1.752.085.482                          
                                                       13 HASH JOIN OUTER PARALLEL_COMBINED_WITH_PARENT :Q1004Cost: 4,86 Bytes: 647.395.154 Cardinality: 13.212.146                     
                                                            8 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1004Cost: 1,961 Bytes: 158.611.600 Cardinality: 6.344.464                
                                                                 7 PX SEND HASH PARALLEL_TO_PARALLEL SYS.:TQ10001 :Q1001Cost: 1,961 Bytes: 158.611.600 Cardinality: 6.344.464           
                                                                      6 PX BLOCK ITERATOR PARALLEL_COMBINED_WITH_CHILD :Q1001Cost: 1,961 Bytes: 158.611.600 Cardinality: 6.344.464      
                                                                           5 TABLE ACCESS FULL TABLE PARALLEL_COMBINED_WITH_PARENT STGADMIN.DELTA_SERVICE_HISTORY :Q1001Cost: 1,961 Bytes: 158.611.600 Cardinality: 6.344.464
                                                            12 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1004Cost: 2,879 Bytes: 368.690.880 Cardinality: 15.362.120                
                                                                 11 PX SEND HASH PARALLEL_TO_PARALLEL SYS.:TQ10002 :Q1002Cost: 2,879 Bytes: 368.690.880 Cardinality: 15.362.120           
                                                                      10 PX BLOCK ITERATOR PARALLEL_COMBINED_WITH_CHILD :Q1002Cost: 2,879 Bytes: 368.690.880 Cardinality: 15.362.120      
                                                                           9 TABLE ACCESS FULL TABLE PARALLEL_COMBINED_WITH_PARENT STGADMIN.STG_HIERARCHY :Q1002Cost: 2,879 Bytes: 368.690.880 Cardinality: 15.362.120
                                                       17 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1004Cost: 28,982 Bytes: 5.838.854.928 Cardinality: 243.285.622                     
                                                            16 PX SEND HASH PARALLEL_TO_PARALLEL SYS.:TQ10003 :Q1003Cost: 28,982 Bytes: 5.838.854.928 Cardinality: 243.285.622                
                                                                 15 PX BLOCK ITERATOR PARALLEL_COMBINED_WITH_CHILD :Q1003Cost: 28,982 Bytes: 5.838.854.928 Cardinality: 243.285.622           
                                                                      14 TABLE ACCESS FULL TABLE PARALLEL_COMBINED_WITH_PARENT STGADMIN.DELTA_ACCOUNT :Q1003Cost: 28,982 Bytes: 5.838.854.928 Cardinality: 243.285.622      
...second query
Plan
MERGE STATEMENT ALL_ROWSCost: 3.521.711 Bytes: 291.687.979.305 Cardinality: 652.545.815                                                                                                     
     32 MERGE STGADMIN.STG_HIERARCHY                                                                                                
          31 PX COORDINATOR                                                                                           
               30 PX SEND QC (RANDOM) PARALLEL_TO_SERIAL SYS.:TQ10006 :Q1006Cost: 3.521.711 Bytes: 176.839.915.865 Cardinality: 652.545.815                                                                                      
                    29 VIEW PARALLEL_COMBINED_WITH_PARENT STGADMIN. :Q1006                                                                                
                         28 HASH JOIN RIGHT OUTER BUFFERED PARALLEL_COMBINED_WITH_PARENT :Q1006Cost: 3.521.711 Bytes: 176.839.915.865 Cardinality: 652.545.815                                                                            
                              4 BUFFER SORT PARALLEL_COMBINED_WITH_CHILD :Q1006                                                                      
                                   3 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1006Cost: 20,828 Bytes: 860.278.720 Cardinality: 15.362.120                                                                  
                                        2 PX SEND HASH PARALLEL_FROM_SERIAL SYS.:TQ10000 Cost: 20,828 Bytes: 860.278.720 Cardinality: 15.362.120                                                             
                                             1 TABLE ACCESS FULL TABLE STGADMIN.STG_HIERARCHY Cost: 20,828 Bytes: 860.278.720 Cardinality: 15.362.120                                                        
                              27 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1006Cost: 3.500.345 Bytes: 140.125.783.665 Cardinality: 651.747.831                                                                       
                                   26 PX SEND HASH PARALLEL_TO_PARALLEL SYS.:TQ10005 :Q1005Cost: 3.500.345 Bytes: 140.125.783.665 Cardinality: 651.747.831                                                                  
                                        25 VIEW PARALLEL_COMBINED_WITH_PARENT STGADMIN. :Q1005Cost: 3.500.345 Bytes: 140.125.783.665 Cardinality: 651.747.831                                                             
                                             24 SORT UNIQUE PARALLEL_COMBINED_WITH_PARENT :Q1005Cost: 3.500.345 Bytes: 121.225.096.566 Cardinality: 651.747.831                                                        
                                                  23 VIEW PARALLEL_COMBINED_WITH_PARENT STGADMIN. :Q1005Cost: 1.195.554 Bytes: 121.225.096.566 Cardinality: 651.747.831                                                   
                                                       22 WINDOW SORT PUSHED RANK PARALLEL_COMBINED_WITH_PARENT :Q1005Cost: 1.195.554 Bytes: 58.005.556.959 Cardinality: 651.747.831                                              
                                                            21 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1005Cost: 1.195.554 Bytes: 58.005.556.959 Cardinality: 651.747.831                                         
                                                                 20 PX SEND HASH PARALLEL_TO_PARALLEL SYS.:TQ10004 :Q1004Cost: 1.195.554 Bytes: 58.005.556.959 Cardinality: 651.747.831                                    
                                                                      19 WINDOW CHILD PUSHED RANK PARALLEL_COMBINED_WITH_PARENT :Q1004Cost: 1.195.554 Bytes: 58.005.556.959 Cardinality: 651.747.831                               
                                                                           18 HASH JOIN OUTER PARALLEL_COMBINED_WITH_PARENT :Q1004Cost: 34,402 Bytes: 58.005.556.959 Cardinality: 651.747.831                          
                                                                                13 HASH JOIN OUTER PARALLEL_COMBINED_WITH_PARENT :Q1004Cost: 4,859 Bytes: 319.455.955 Cardinality: 4.914.707                     
                                                                                     8 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1004Cost: 1,963 Bytes: 152.576.580 Cardinality: 3.721.380                
                                                                                          7 PX SEND HASH PARALLEL_TO_PARALLEL SYS.:TQ10001 :Q1001Cost: 1,963 Bytes: 152.576.580 Cardinality: 3.721.380           
                                                                                               6 PX BLOCK ITERATOR PARALLEL_COMBINED_WITH_CHILD :Q1001Cost: 1,963 Bytes: 152.576.580 Cardinality: 3.721.380      
                                                                                                    5 TABLE ACCESS FULL TABLE PARALLEL_COMBINED_WITH_PARENT STGADMIN.DELTA_SERVICE_HISTORY :Q1001Cost: 1,963 Bytes: 152.576.580 Cardinality: 3.721.380
                                                                                     12 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1004Cost: 2,879 Bytes: 368.690.880 Cardinality: 15.362.120                
                                                                                          11 PX SEND HASH PARALLEL_TO_PARALLEL SYS.:TQ10002 :Q1002Cost: 2,879 Bytes: 368.690.880 Cardinality: 15.362.120           
                                                                                               10 PX BLOCK ITERATOR PARALLEL_COMBINED_WITH_CHILD :Q1002Cost: 2,879 Bytes: 368.690.880 Cardinality: 15.362.120      
                                                                                                    9 TABLE ACCESS FULL TABLE PARALLEL_COMBINED_WITH_PARENT STGADMIN.STG_HIERARCHY :Q1002Cost: 2,879 Bytes: 368.690.880 Cardinality: 15.362.120
                                                                                17 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1004Cost: 28,982 Bytes: 5.838.854.928 Cardinality: 243.285.622                     
                                                                                     16 PX SEND HASH PARALLEL_TO_PARALLEL SYS.:TQ10003 :Q1003Cost: 28,982 Bytes: 5.838.854.928 Cardinality: 243.285.622                
                                                                                          15 PX BLOCK ITERATOR PARALLEL_COMBINED_WITH_CHILD :Q1003Cost: 28,982 Bytes: 5.838.854.928 Cardinality: 243.285.622           
                                                                                               14 TABLE ACCESS FULL TABLE PARALLEL_COMBINED_WITH_PARENT STGADMIN.DELTA_ACCOUNT :Q1003Cost: 28,982 Bytes: 5.838.854.928 Cardinality: 243.285.622

Similar Messages

  • Self join or analytical function ..?

    Hi
    I have a table data like this:
    select * from s_table;
    ID ADDRESS ADDRESS1 CODE
    a1 Dallas Tx 10
    b1 DFW Tx 20
    c1 Houston Tx 30
    d1 Elpaso Tx 40
    e1 Austin Tx 50
    e1 Austin Tx 51
    6 rows selected
    Business rule:
    I want to concatenate code with a space in between when ID matches previous Id's
    so here my output should look like:
    ID ADDRESS ADDRESS1 CODE
    a1 Dallas Tx 10
    b1 DFW Tx 20
    c1 Houston Tx 30
    d1 Elpaso Tx 40
    e1 Austin Tx 50 51
    any help is appreciated:
    Thanks
    JP

    with t as (
               select 'a1' ID,'Dallas' ADDRESS,'Tx' ADDRESS1,10 CODE from dual union all
               select 'b1','DFW','Tx',20 from dual union all
               select 'c1','Houston','Tx',30 from dual union all
               select 'd1','Elpaso','Tx',40 from dual union all
               select 'e1','Austin','Tx',50 from dual union all
               select 'e1','Austin','Tx',51 from dual
    select  id,
            address,
            address1,
            ltrim(sys_connect_by_path(code,' ')) code_list
      from  (
             select  id,
                     address,
                     address1,
                     code,
                     row_number() over(partition by id,address,address1 order by code) rn,
                     count(*) over(partition by id,address,address1) cnt
               from  t
      where rn = cnt
      start with rn = 1
      connect by id = prior id
             and address = prior address
             and address1 = prior address1
             and rn = prior rn + 1
    ID ADDRESS AD CODE_LIST
    a1 Dallas  Tx 10
    b1 DFW     Tx 20
    c1 Houston Tx 30
    d1 Elpaso  Tx 40
    e1 Austin  Tx 50 51
    SQL> SY.

  • Self Join and Aggregate Functions

    Hi all,
    I am trying a query in TOAD where I need to use an aggregate function MAX and a self join using a subquery. Its working fine when there is no aggregate function but when I tried to use the MAX function then its running for infi time. Is it because of the invalid joins? or because of the usage of the self join and aggregate func?
    The query contains some other tables too....
    Any one please help....
    Thanks in advance,
    G

    Toad will bring back a limited set of rows and present them to you giving the impression that the work is done. Adding an aggregate function requires the entire resultset to be traversed.
    Yes, post the 2 queries to verify what I am saying.

  • Self-join query to Analytical function query

    Hi All,
    I have converted a self-join query to Analytical function query due to the performance reasons.
    Query which is using Analytical function is giving the correct count as I compared it with query using Self-Join. Can you please tell what is wrong in the query.
    ==========================
    Query using Self-Join
    select count(1)
    From (select t1.*, max(t1.dw_creation_dt) over (partition by t1.empl_id) pers_max_date
    from ohr_pers_curr t1 ) pers
         , (select t2.*, max(t2.dw_creation_dt) over (partition by t2.empl_id, t2.empl_rcd) job_max_date
         from OHR_JOB_CURR t2) job
    where pers.empl_id = job.empl_id
    and pers.dw_creation_dt=pers.pers_max_date
    and job.dw_creation_dt=job.job_max_date
    and job.dummy_row_flag = 'N'
    and pers.dw_creation_rsn_cd in ('N', 'U')
    and job.dw_creation_rsn_cd in ('N', 'U')
    ================================================
    Query Using Analytical function
    select count(1)
    From (select t1.*, max(t1.dw_creation_dt) over (partition by t1.empl_id) pers_max_date
    from ohr_pers_curr t1 ) pers
         , (select t2.*, max(t2.dw_creation_dt) over (partition by t2.empl_id, t2.empl_rcd) job_max_date
         from OHR_JOB_CURR t2) job
    where pers.empl_id = job.empl_id
    and pers.dw_creation_dt=pers.pers_max_date
    and job.dw_creation_dt=job.job_max_date
    and job.dummy_row_flag = 'N'
    and pers.dw_creation_rsn_cd in ('N', 'U')
    and job.dw_creation_rsn_cd in ('N', 'U')
    ==================================

    Hi David,
    The base is same but the problem is different.
    As far as implementation concern these queries looks same, but when I see there counts, they do not match. I do not have any reason why this happening.
    Regards
    Gaurav

  • Analytic function question

    I have been trying to fix this since yesterday ad I am close, here is the question
    CREATE TABLE P_X_STG
      PID  NUMBER,
      EID  VARCHAR2(10 BYTE)
    CREATE TABLE TAB_C
      EID  VARCHAR2(10 BYTE),
      X    NUMBER
    SET DEFINE OFF;
    Insert into P_X_STG
       (PID, EID)
    Values
       (1, 'e3');
    Insert into P_X_STG
       (PID, EID)
    Values
       (1, 'e1');
    Insert into P_X_STG
       (PID, EID)
    Values
       (1, 'e2');
    Insert into P_X_STG
       (PID, EID)
    Values
       (2, 'e3');
    Insert into P_X_STG
       (PID, EID)
    Values
       (2, 'e4');
    Insert into P_X_STG
       (PID, EID)
    Values
       (3, 'e5');
    COMMIT;
    SET DEFINE OFF;
    Insert into TAB_C
       (EID, X)
    Values
       ('e1', 100);
    Insert into TAB_C
       (EID, X)
    Values
       ('e3', 300);
    Insert into TAB_C
       (X)
    Values
       (400);
    COMMIT;we match both the tables by eid
    if the eid matches, get corresponding x information
    if a pid has multiple different eids, for matching eid, get corresponding x information, but for non matching, simply put a NULL to x
    for matching eids, print "ematch", for non matchig eids, print "pmatch"
    in the below query, for non matching eids, we copy x information from matching ones, can someone help me substitute tha with Null
    SELECT pid,
                eid,
                x,
                ematch
           FROM (  SELECT p.pid,
                          p.eid,
                          CASE
                             WHEN p.eid = c.eid THEN c.x
                             ELSE LAG (x) OVER (ORDER BY pid)
                          END
                             x,
                          CASE WHEN p.eid = c.eid THEN 'ematch' ELSE 'pmatch' END
                             ematch
                     FROM p_x_stg p, tab_c c
                    WHERE p.eid = c.eid(+)
                 ORDER BY pid, eid)
          WHERE x IS NOT NULL
       ORDER BY pid;
    result is
    1     e1     100     ematch
    1     e2     100     pmatch
    1     e3     300     ematch
    2     e3     300     ematch
    2     e4     300     pmatchI want below result
    1    e1    100    ematch
    1    e2    null    pmatch
    1    e3    300    ematch
    2    e3    300    ematch
    2    e4    null    pmatchfor 1, e2 and 2, e4, instead of copying, just put null
    Edited by: user650888 on Apr 6, 2012 12:29 PM

    Hi,
    user650888 wrote:
    thanks, can this be combined into one query ?Do you mean without a sub-query?
    Why? What's wrong with a sub-query?
    If uisng a sub-query is the clearest and most efficient way to get the results you want, why wouldn't you want to use a sub-query?
    Suppose there was a way, that involved some other technique. How would we know that you didn't object to that technique as well?
    At any rate, I don't see how to do it without a sub-query. Analytic functions are computed after the WHERE clause is applied. If you want to use the results of an analytic function in a WHERE clause (as here, we want to use the results of the analytic COUNT function in a WHERE clause), then you have to compute the analytic function separately, in another (sub-) query.

  • Help - tuning analytic functions

    Where can I find information on hints to speed up oracle analytic functions?
    The table xyz has about 12 million rows.
    The col1 / col2 combinations are effective dated and have an Active /Inactive status.
    Table xyz: col1, col2, effdt, status.
    Goal: I want to eliminate the col1 / col2 combinations where all rows over time have an Inactive status.
    The sq I wrote looks like this:
    select * from
    select col1, col2,
    SUM (1) OVER (PARTITION BY f.col1, f.col2 ORDER BY fcol1, f.col2) total_cnt,
    SUM (CASE WHEN f.status = 'I' THEN 1 ELSE 0 END) OVER
    (PARTITION BY f.col1, f.col2 ORDER BY f.col1) inactive_cnt
    from table xyz f
    where total_cnt > inactive_cnt
    Thanks,
    Frank

    Have a look at these standard threads:
    How to post a tuning request:
    HOW TO: Post a SQL statement tuning request - template posting
    When your query takes too long:
    When your query takes too long ...

  • Tuning sql with analytic function

    Dear friends I've developed one sql :
    with REP as
    (select /*+ MATERIALIZE */ branch_code,
       row_number() over(partition by branch_code, account order by bkg_date desc  ) R,
             account,
             bkg_date,
             lcy_closing_bal
        from history t
    select REP1.branch_code,
           REP1.account,
           REP1.bkg_date,
           REP1.lcy_closing_bal,
             NULL  AS second,
           REP2.bkg_date        bkg_date2,
           REP2.lcy_closing_bal lcy_closing_bal2,
            NULL  AS third,
           REP3.bkg_date        bkg_date3,
           REP3.lcy_closing_bal lcy_closing_bal3
      from (SELECT * FROM REP WHERE R=1) REP1, (SELECT * FROM REP WHERE R=2) REP2, (SELECT * FROM REP WHERE R=3) REP3
    where
           (REP1.BRANCH_CODE = REP2.BRANCH_CODE(+) AND REP1.ACCOUNT = REP2.ACCOUNT(+)) AND
           (REP1.BRANCH_CODE = REP3.BRANCH_CODE(+) AND REP1.ACCOUNT = REP3.ACCOUNT(+))The point is I want to restrict (tune) REP before it used ,because , as you can see I need maximum three value from REP (where R=1,R=2,R=3) . Which analytic function and with wich options I have to use to receive only 3 values in each branch_code,account groups at the materializing time ?

    Radrigez wrote:
    Dear friends I've developed one sql :
    with REP as
    from (SELECT * FROM REP WHERE R=1) REP1,
    (SELECT * FROM REP WHERE R=2) REP2,
    (SELECT * FROM REP WHERE R=3) REP3
    where
    (REP1.BRANCH_CODE = REP2.BRANCH_CODE(+) AND REP1.ACCOUNT = REP2.ACCOUNT(+)) AND
    (REP1.BRANCH_CODE = REP3.BRANCH_CODE(+) AND REP1.ACCOUNT = REP3.ACCOUNT(+))
    The first step is to put your subquery (which doesn't need to be materialized) into an inline view and restrict the result set on r in (1,2,3) as suggested by thtsang - you don't need to query the same result set three times.
    Then you're looking at a simple pivot operation (assuming the number of rows you want per branch and account is fixed). If you're on 11g search the manuals for PIVOT, on earlier versions you can do this with a decode() or case() operator.
    Step 1 (which could go into another factored subquery) would be something like:
    select
            branch_code, account,
            case r = 1 then bkg_date end bkg_date,
            case r = 1 then lcy_closing_bal end lcy_closing_bal,
            case r = 2 then bkg_date end bkg_date2,
            case r = 2 then lcy_closing_bal end lcy_closing_bal2,
            case r = 3 then bkg_date end bkg_date3,
            case r = 3 then lcy_closing_bal end lcy_closing_bal3
    from
            repThis gives you the eight necessary columns, but still (up to) three rows per branch/account.
    Then you aggregate this (call it rep1) on branch and account.
    select
            branch_code, account,
            max(bkg_date),
            max(lcy_closing_bal),
            max(bkg_date2),
            max(lcy_closing_bal2),
            max(bkg_date3),
            max(lcy_closing_bal3)
    from
            rep1
    group by
            branch_code, account
    order by
            branch_code, accountRegards
    Jonathan Lewis
    http://jonathanlewis.wordpress.com
    Author: <b><em>Oracle Core</em></b>

  • Question for analytic functions experts

    Hi,
    I have an ugly table containing an implicit master detail relation.
    The table can be ordered by sequence and then each detail is beneath it's master (in sequence).
    If it is a detail, the master column is NULL and vice versa.
    Sample:
    SEQUENCE MASTER DETAIL BOTH_PRIMARY_KEYS
    1____________A______________1
    2___________________A_______1
    3___________________B_______2
    4____________B______________2
    5___________________A_______3
    6___________________B_______4
    Task: Go into the table with the primary key of my detail, and search the primary key of it's master.
    I already have a solution how to get it, but I would like to know if there is an analytic statement,
    which is more elegant, instead of selfreferencing my table three times. Somebody used to analytic functions?
    Thanks,
    Dirk

    Hi,
    Do you mean like this?
    with data as (
    select 1 sequence, 'A' master, null detail, 1 both_primary_keys from dual union all
    select 2, null, 'A', 1 from dual union all
    select 3, null, 'B', 2 from dual union all
    select 4, 'B', null, 2 from dual union all
    select 5, null, 'A', 3 from dual union all
    select 6, null, 'B', 4 from dual )
    select (select max(both_primary_keys) keep (dense_rank last order by sequence)
            from data
            where sequence < detail_record.sequence and detail is null) master_primary_key
    from data detail_record
    where (both_primary_keys=3 /*lookup detail key 3 */  and master is null)

  • Oracle Analytic function tuning

    Hi all,
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
    PL/SQL Release 10.2.0.3.0 - Production
    CORE 10.2.0.3.0 Production
    TNS for IBM/AIX RISC System/6000: Version 10.2.0.3.0 - Productio
    NLSRTL Version 10.2.0.3.0 - Production
    I have a query which has analytic function uses large space on temporary tablespace resulting in direct path temp read and direct path temp write wait events taking too much time. Is there any way to tune such query?
    Thanks in advance.

    user9074365 wrote:
    Hi all,
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
    PL/SQL Release 10.2.0.3.0 - Production
    CORE 10.2.0.3.0 Production
    TNS for IBM/AIX RISC System/6000: Version 10.2.0.3.0 - Productio
    NLSRTL Version 10.2.0.3.0 - Production
    I have a query which has analytic function uses large space on temporary tablespace resulting in direct path temp read and direct path temp write wait events taking too much time. Is there any way to tune such query?
    With your version of Oracle, and high-volumes of data going through analytic function, it's likely that this blog note applies. You may need an upgrade or special patch. http://jonathanlewis.wordpress.com/2009/09/07/analytic-agony/
    Regards
    Jonathan Lewis

  • Performance Tuning - Self Join Issue

    Hi,
    The following query takes long time to execute. Is there any better way to
    re-writing the query to reduce the time it takes to execute.
    INSERT INTO TT_TEMP_MAINGUI_SP_PERCENT_MOV
    (prev_prc_dt,asset_id,pricing_pt_id,price_dt)
    SELECT max(tpm2.prc_dt),
    tpm2.asset_id ,
    tpm2.pricing_pt_id ,
    tpm1.prc_dt
    FROM t_prc_master tpm1,
    t_prc_master tpm2
    WHERE tpm1.prc_dt = '19-Dec-07'
    AND tpm1.asset_id = tpm2.asset_id
    AND tpm1.pricing_pt_id = tpm2.pricing_pt_id
    AND tpm2.prc_dt < tpm1.prc_dt
    AND tpm2.accept_flg = 'Y'
    AND tpm1.accept_flg = 'Y'
    AND EXISTS (SELECT 1 FROM t_temp_prcmov
    WHERE pca_flg = 'P'
    AND tpm1.pricing_pt_id = prc_pt_cntry_atyp)
    GROUP BY tpm2.asset_id, tpm2.pricing_pt_id,tpm1.prc_dt;
    select count(*) from t_prc_master
    where prc_dt = '19-Dec-07'
    COUNT(*)
    784161
    -- Here is the TKPROF Output
    INSERT INTO TT_TEMP_MAINGUI_SP_PERCENT_MOV
    (prev_prc_dt,asset_id,pricing_pt_id,price_dt)
    SELECT max(tpm2.prc_dt),
    tpm2.asset_id ,
    tpm2.pricing_pt_id ,
    tpm1.prc_dt
    FROM t_prc_master tpm1,
    t_prc_master tpm2
    WHERE tpm1.prc_dt = '19-Dec-07'
    AND tpm1.asset_id = tpm2.asset_id
    AND tpm1.pricing_pt_id = tpm2.pricing_pt_id
    AND tpm2.prc_dt < tpm1.prc_dt
    AND tpm2.accept_flg = 'Y'
    AND tpm1.accept_flg = 'Y'
    AND EXISTS (SELECT 1 FROM t_temp_prcmov
    WHERE pca_flg = 'P'
    AND tpm1.pricing_pt_id = prc_pt_cntry_atyp)
    GROUP BY tpm2.asset_id, tpm2.pricing_pt_id,tpm1.prc_dt
    call count cpu elapsed disk query current rows
    Parse 1 0.00 0.00 0 0 0 0
    Execute 1 226.01 317.50 1980173 4915655 805927 780544
    Fetch 0 0.00 0.00 0 0 0 0
    total 2 226.01 317.51 1980173 4915655 805927 780544
    Misses in library cache during parse: 1
    Optimizer goal: CHOOSE
    Parsing user id: 98 (PRSDBO)
    Rows Row Source Operation
    780544 SORT GROUP BY (cr=4915236 r=1980165 w=0 time=312751120 us)
    40416453 NESTED LOOPS (cr=4915236 r=1980165 w=0 time=245408132 us)
    783459 NESTED LOOPS (cr=956325 r=92781 w=0 time=17974163 us)
    55 TABLE ACCESS FULL T_TEMP_PRCMOV (cr=3 r=0 w=0 time=406 us)
    783459 TABLE ACCESS BY INDEX ROWID T_PRC_MASTER (cr=956322 r=92781 w=0 time=17782856 us)
    784161 INDEX RANGE SCAN PRC_DT_ASSET_ID (cr=412062 r=69776 w=0 time=14136725 us)(object id 450059)
    40416453 INDEX RANGE SCAN ASSET_DT_ACCEPT_FLG (cr=3958911 r=1887384 w=0 time=217215303 us)(object id 450055)
    Rows Execution Plan
    0 INSERT STATEMENT GOAL: CHOOSE
    780544 SORT (GROUP BY)
    40416453 NESTED LOOPS
    783459 NESTED LOOPS
    55 TABLE ACCESS GOAL: ANALYZED (FULL) OF 'T_TEMP_PRCMOV'
    783459 TABLE ACCESS GOAL: ANALYZED (BY INDEX ROWID) OF
    'T_PRC_MASTER'
    784161 INDEX GOAL: ANALYZED (RANGE SCAN) OF 'PRC_DT_ASSET_ID'
    (NON-UNIQUE)
    40416453 INDEX GOAL: ANALYZED (RANGE SCAN) OF 'ASSET_DT_ACCEPT_FLG'
    (UNIQUE)
    Could somebody help me in resolving the issue? It would be appreciated...

    Well, it's a bit of a mess to read. Please use the pre or code tags enclosed in [] next time to preserve the formatting of the code.
    First thing that looks 'bad' to me is
    WHERE tpm1.prc_dt = '19-Dec-07'which should be (i assume you want 2007 and not 1907)
    WHERE tpm1.prc_dt = TO_DATE('19-Dec-2007', 'DD-MON-YYYY');The next thing i'm very confused with is...why are you self joining the table? You should be able to just do this.....logically, it should produce the same results, though it's obviously not tested :D)
    SELECT
       max(tpm2.prc_dt),
       tpm2.asset_id ,
       tpm2.pricing_pt_id ,
       TO_DATE('19-Dec-2007', 'DD-MON-YYYY')  AS prc_dt
    FROM t_prc_master tpm2
    WHERE tpm2.prc_dt < TO_DATE('19-Dec-2007', 'DD-MON-YYYY')
    AND   tpm2.accept_flg = 'Y'
    AND   EXISTS   ( 
                      SELECT
                         NULL
                      FROM t_prc_master tpm1
                      WHERE tpm1.prc_dt          = TO_DATE('19-Dec-2007', 'DD-MON-YYYY')
                      AND   tpm1.asset_id        = tpm2.asset_id
                      AND   tpm1.pricing_pt_id   = tpm2.pricing_pt_id
                      AND   tpm1.accept_flg      = 'Y'                 
                      AND   tpm1.pricing_pt_id  IN (SELECT tmov.prc_pt_cntry_atyp FROM t_temp_prcmov tmov WHERE tmov.pca_flg = 'P')
    GROUP BY tpm2.asset_id, tpm2.pricing_pt_id, TO_DATE('19-Dec-2007', 'DD-MON-YYYY');Message was edited by:
    Tubby

  • Yet another question on analytical functions

    Hi,
    A "simple" issue with date overlapping: I need to display a "y" or "n" where a start time/end time overlaps with another row for the same employee.
    For this, I used analytical functions to find out which rows were overlapping. This is what I did:
    create table test (id number, emp_id number, date_worked date, start_time date, end_time date);
    insert into test values (1, 333, to_date('2008-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),to_date('2008-01-01 08:00:00', 'YYYY-MM-DD HH24:MI:SS'),to_date('2008-01-01 10:00:00', 'YYYY-MM-DD HH24:MI:SS'));
    insert into test values (2, 333, to_date('2008-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),to_date('2008-01-01 10:00:00', 'YYYY-MM-DD HH24:MI:SS'),to_date('2008-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'));
    insert into test values (3, 444, to_date('2008-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),to_date('2008-01-01 08:00:00', 'YYYY-MM-DD HH24:MI:SS'),to_date('2008-01-01 09:00:00', 'YYYY-MM-DD HH24:MI:SS'));
    insert into test values (4, 333, to_date('2008-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),to_date('2008-01-01 11:00:00', 'YYYY-MM-DD HH24:MI:SS'),to_date('2008-01-01 13:00:00', 'YYYY-MM-DD HH24:MI:SS'));
    SQL> select * from test;
            ID     EMP_ID DATE_WORKED         START_TIME          END_TIME
             1        333 01-01-2008 00:00:00 01-01-2008 08:00:00 01-01-2008 10:00:00 
             2        333 01-01-2008 00:00:00 01-01-2008 10:00:00 01-01-2008 12:00:00  --> conflict
             3        444 01-01-2008 00:00:00 01-01-2008 08:00:00 01-01-2008 09:00:00
             4        333 01-01-2008 00:00:00 01-01-2008 11:00:00 01-01-2008 13:00:00  --> conflict Here I see that Employee 333 is scheduled from 10 to 12 am, but it's also scheduled from 11 to 13. This is a conflict.
    To find this conflict, I did this (please correct me if this is incorrect):
    SQL> select id, emp_id, date_worked, start_time, end_time, next_date
      2  from (
      3        select lead( start_time, 1 ) over ( partition by emp_id order by start_time ) next_date,
      4               start_time, end_time, emp_id, date_worked, id
      5        from   test
      6       )
      7  where  next_date < end_time;
            ID     EMP_ID DATE_WORKED         START_TIME          END_TIME            NEXT_DATE
             2        333 01-01-2008 00:00:00 01-01-2008 10:00:00 01-01-2008 12:00:00 01-01-2008 11:00:00So far, so good. Where I'm stuck is, I need to display the conflicts in this way:
            ID     EMP_ID DATE_WORKED         START_TIME          END_TIME            OVERLAPPED
             1        333 01-01-2008 00:00:00 01-01-2008 08:00:00 01-01-2008 10:00:00
             2        333 01-01-2008 00:00:00 01-01-2008 10:00:00 01-01-2008 12:00:00 yes
             3        444 01-01-2008 00:00:00 01-01-2008 08:00:00 01-01-2008 09:00:00
             4        333 01-01-2008 00:00:00 01-01-2008 11:00:00 01-01-2008 13:00:00 yes Is there a way I can achieve this?
    I tried doing a nested query with a count(*) but no success...

    I found an issue. Say we insert this row.
    insert into test values (5, 333, to_date('2008-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),to_date('2008-01-01 07:00:00', 'YYYY-MM-DD HH24:MI:SS'),to_date('2008-01-01 08:00:00', 'YYYY-MM-DD HH24:MI:SS'));
    SQL> select * from test order by start_time;
            ID     EMP_ID DATE_WORKED         START_TIME          END_TIME
             5        333 01-01-2008 00:00:00 01-01-2008 07:00:00 01-01-2008 08:00:00
             1        333 01-01-2008 00:00:00 01-01-2008 08:00:00 01-01-2008 10:00:00
             3        444 01-01-2008 00:00:00 01-01-2008 08:00:00 01-01-2008 09:00:00
             2        333 01-01-2008 00:00:00 01-01-2008 10:00:00 01-01-2008 12:00:00 --> conflict
             4        333 01-01-2008 00:00:00 01-01-2008 11:00:00 01-01-2008 13:00:00 --> conflictIf I run the SQL suggested by Nicloei, I get:
    SQL> select id,
      2         emp_id,
      3         date_worked,
      4         start_time,
      5         end_time,
      6           Case When end_time > prev_date Or next_date > End_time Then 'Yes' Else 'No' End over_lap
      7      from (
      8            select lead( start_time, 1 ) over ( partition by emp_id order by start_time ) next_date,
      9                   lag( start_time, 1 ) over ( partition by emp_id order by start_time ) prev_date,
    10                   start_time, end_time, emp_id, date_worked, id
    11            from   test
    12           );
            ID     EMP_ID DATE_WORKED         START_TIME          END_TIME            OVER_LAP
             5        333 01-01-2008 00:00:00 01-01-2008 07:00:00 01-01-2008 08:00:00 No
             1        333 01-01-2008 00:00:00 01-01-2008 08:00:00 01-01-2008 10:00:00 Yes
             2        333 01-01-2008 00:00:00 01-01-2008 10:00:00 01-01-2008 12:00:00 Yes
             4        333 01-01-2008 00:00:00 01-01-2008 11:00:00 01-01-2008 13:00:00 Yes
             3        444 01-01-2008 00:00:00 01-01-2008 08:00:00 01-01-2008 09:00:00 NoIt's basically saying that there's an overlap between id 1 and id 2 (8-10 and 10-12), which is incorrect. I ran the inner query by itself:
    SQL> select lead( start_time, 1 ) over ( partition by emp_id order by start_time ) next_date,
      2                   lag( start_time, 1 ) over ( partition by emp_id order by start_time ) prev_date,
      3                   start_time, end_time, emp_id, date_worked, id
      4            from   test;
    NEXT_DATE           PREV_DATE           START_TIME          END_TIME                EMP_ID DATE_WORKED                 ID
    01-01-2008 08:00:00                     01-01-2008 07:00:00 01-01-2008 08:00:00        333 01-01-2008 00:00:00          5
    01-01-2008 10:00:00 01-01-2008 07:00:00 01-01-2008 08:00:00 01-01-2008 10:00:00        333 01-01-2008 00:00:00          1
    01-01-2008 11:00:00 01-01-2008 08:00:00 01-01-2008 10:00:00 01-01-2008 12:00:00        333 01-01-2008 00:00:00          2
                        01-01-2008 10:00:00 01-01-2008 11:00:00 01-01-2008 13:00:00        333 01-01-2008 00:00:00          4
                                            01-01-2008 08:00:00 01-01-2008 09:00:00        444 01-01-2008 00:00:00          3with this data, I can't seem to find a way to conditional test in order to print my overlap column with "yes" or "no" accordingly.
    am I missing something?

  • Oracle query with out using self join

    hi friends,
    i have one table for exeample PERSTATUS
    pk/fK STUDENT NUMBER SUBJECT MARKS STATUS
    1 ACCOUNTS 15 RED
    1 MATHS 35 YELLOW
    1 SCINECE 45 GREEN
    2 ACCOUNTS 55 BROWN
    2 MATHS 35 YELLOW
    2 SCINECE 45 GREEN
    3 ACCOUNTS 15 RED
    3 MATHS 35 YELLOW
    3 SCINECE 45 GREEN
    i want students how status is both red and yellow so i am using self join
    i want students status is both red and yellow so i am using self join
    SELECT PS.STUDENTNUMBER,PS.STATUS,PS.STATUS1 FROM PERSTATUS PS ,PERSTATUS PS1
    WHERE PS.STUDENTNUMBER-PS1.STUDENTNUMER
    PS.STATUS='RED' AND PS1.STAUTS='YELLOW'
    i want students status is both RD and YELLOW AND GREEN so i am using self join( two self joinS}
    SELECT PS.STUDENTNUMBER,PS.STATUS,PS.STATUS,PS2.STATUS FROM PERSTATUS PS ,PERSTATUS PS1,PERSTATUS PS2
    WHERE PS.STUDENTNUMBER-PS1.STUDENTNUMER AND PS.STUDENTNUMBER-PS2.STUDENTNUMBER
    PS.STATUS='RED' AND PS1.STAUTS='YELLOW' AND PS2.STAUTUS='GREEN'
    if i require MORE STATUS then more self joins required, is there any alternative to achive this
    and if results comes in multiple rows are accepted (since with the above query result will come in single row)
    i tried to use group by (studentnumber,status) with status='red' and status='yellow'
    but it is not possible could you povidet he solution

    Hi,
    Whenever you have a problem, please post CREATE TABLE and INSERT statements for your sample data, and the exact results you want from that data. Explain how you get those results from that data.
    See the forum FAQ {message:id=9360002}
    Here's an example of how to post the sample data:
    CREATE TABLE     perstatus
    (       studentnumber     NUMBER
    ,     subject          VARCHAR2 (10)
    ,     marks          NUMBER
    ,     status          VARCHAR2 (10)
    INSERT INTO perstatus (studentnumber, subject,    marks, status)
           VALUES           (1,           'ACCOUNTS', 15,       'RED');
    INSERT INTO perstatus (studentnumber, subject  ,  marks, status)
           VALUES           (1,           'MATHS',        35,       'YELLOW');
    INSERT INTO perstatus (studentnumber, subject,    marks, status)
           VALUES           (1,           'SCINECE',  45,       'GREEN');
    INSERT INTO perstatus (studentnumber, subject,    marks, status)
           VALUES           (2,           'ACCOUNTS', 55,       'BROWN');
    INSERT INTO perstatus (studentnumber, subject  ,  marks, status)
           VALUES           (2,           'MATHS',        35,       'YELLOW');
    INSERT INTO perstatus (studentnumber, subject,    marks, status)
           VALUES           (2,           'SCINECE',  45,       'GREEN');
    INSERT INTO perstatus (studentnumber, subject,    marks, status)
           VALUES           (3,           'ACCOUNTS', 15,       'RED');
    INSERT INTO perstatus (studentnumber, subject  ,  marks, status)
           VALUES           (3,           'MATHS',        35,       'YELLOW');
    INSERT INTO perstatus (studentnumber, subject,    marks, status)
           VALUES           (3,           'SCINECE',  45,       'GREEN');You were on the right track, thinking about GROUP BY. You're interested in something about the whole group of rows that has the same studentnumber. Looking at any individual row won't tell you if that row is part of the group you're interested in or not.
    If you want to see information about the group as a whole, you can do the whole job with GROUP BY. In this case, studnetnumber is the only thing that an entire group has in common. If you wanted to see the studentnumbers that had both RED and YELLOW, that is:
    STUDENTNUMBER
                1
                3here's one way you could do it:
    SELECT       studentnumber
    FROM       perstatus
    WHERE       status     IN ('RED', 'YELLOW')
    GROUP BY  studentnumber
    HAVING       COUNT (DISTINCT status) = 2  -- That is, both RED and YELLOW
    ORDER BY  studentnumber
    ;But say you wanted to see details about individuals in the group; for example, say we want to see all the columns for students that have all 3 of RED, YELLOW and GREEN, like this:
    STUDENTNUMBER SUBJECT         MARKS STATUS
                1 SCINECE            45 GREEN
                1 ACCOUNTS           15 RED
                1 MATHS              35 YELLOW
                3 SCINECE            45 GREEN
                3 ACCOUNTS           15 RED
                3 MATHS              35 YELLOWWe used the aggregate COUNT function earlier, but aggregate functions require collapsing the results down to one row per group.
    However, most of the aggregate functions, like COUNT, have analytic counterparts, that can give the same results without collapsing the result set. Here's one way to get the results above, using the analytic COUNT function:
    WITH     got_cnt          AS
         SELECT  studentnumber, subject, marks, status
         ,     COUNT ( DISTINCT CASE
                                   WHEN  status  IN ('RED', 'YELLOW', 'GREEN')
                             THEN  status
                               END
                    ) OVER (PARTITION BY  studentnumber)     AS cnt
         FROM    perstatus
    SELECT    studentnumber, subject, marks, status
    FROM       got_cnt
    WHERE       cnt  = 3
    ORDER BY  studentnumber
    ,            status
    ;

  • Problem with SUM () analytic function

    Dear all,
    Please have a look at my problem.
    SELECT CURR, DT, AMT, RATE,
    SUM(AMT) OVER (PARTITION BY CURR ORDER BY DT) SUMOVER,
    sum( amt * rate) over (PARTITION BY CURR ORDER BY DT) / SUM(AMT) OVER (PARTITION BY CURR ORDER BY DT) avgrt
    FROM
    select 'CHF' CURR, ADD_MONTHS(TO_DATE('01-DEC-07'), LEVEL -1) DT, 100 * LEVEL AMT, 1 +  ( 5* LEVEL/100) RATE
    FROM DUAL CONNECT BY LEVEL < 10
    SQL> /
    CUR DT               AMT       RATE    SUMOVER      AVGRT
    CHF 01-DEC-07        100       1.05        100       1.05
    CHF 01-JAN-08        200        1.1        300 1.08333333
    CHF 01-FEB-08        300       1.15        600 1.11666667
    CHF 01-MAR-08        400        1.2       1000       1.15
    CHF 01-APR-08        500       1.25       1500 1.18333333
    CHF 01-MAY-08        600        1.3       2100 1.21666667
    CHF 01-JUN-08        700       1.35       2800       1.25
    CHF 01-JUL-08        800        1.4       3600 1.28333333
    CHF 01-AUG-08        900       1.45       4500 1.31666667
    Table Revaluation
    select 'CHF' CURR1, '31-DEC-07' DT , 1.08 RATE FROM DUAL UNION ALL
    select 'CHF' CURR1, '31-MAR-08' DT , 1.22 RATE FROM DUAL UNION ALL
    select 'CHF' CURR1, '30-JUN-08' DT , 1.38 RATE FROM DUAL
    CUR DT              RATE
    CHF 31-DEC-07       1.08
    CHF 31-MAR-08       1.22
    CHF 30-JUN-08       1.38.
    Problem is with the calculation of average rate.
    I want to consider the data in the revaluation table to be used in the calculation of
    average rate.
    So average rate for Jan-08 will be
    (100 * 1.08(dec revaluation rate) + 200 * 1.1 ) / (300) = 1.093333333
    for Feb-08
    (100 * 1.08(dec revaluation rate) + 200 * 1.1 + 300 * 1.15) / (600) = 1.121666667
    for mar-08
    (100 * 1.08(dec revaluation rate) + 200 * 1.1 + 300 * 1.15 + 400 * 1.2) / (1000) = 1.153
    for Apr-08
    (1000 * 1.22(Apr revaluation rate) + 500 * 1.25) /1500 = 1.23
    for May-08
    (1000 * 1.22(Apr revaluation rate) + 500 * 1.25 + 600 * 1.30 ) /2100 = 1.25
    and so on..
    Kindly advice

    Hi,
    The main thing in this problem is that for every dt you want to compute the cumulative total from previous rows using the formula
    SUM (amt * rate)
    But rate can be either the rate from the revaluation table or the rate from the main table. For evaluating prior dates, you wnat to use the most recent rate.
    I'm not sure if you can do this using analytic functions. Like Damorgan said, you should use a self-join.
    The query below gives you the results you requested:
    WITH
    revaluation     AS
         SELECT 'CHF' curr1, TO_DATE ('31-DEC-07', 'DD-MON-RR') dt, 1.08 rate     FROM dual     UNION ALL
         SELECT 'CHF' curr1, TO_DATE ('31-MAR-08', 'DD-MON-RR') dt, 1.22 rate     FROM dual     UNION ALL
         SELECT 'CHF' curr1, TO_DATE ('30-JUN-08', 'DD-MON-RR') dt, 1.38 rate     FROM dual
    original_data     AS
         select     'CHF'                              curr
         ,     ADD_MONTHS(TO_DATE('01-DEC-07'), LEVEL -1)     dt
         ,     100 * LEVEL                         amt
         ,     1 + ( 5* LEVEL/100)                    rate
         FROM     dual
         CONNECT BY     LEVEL < 10
    two_rates     AS
         SELECT     od.*
              SELECT     MAX (dt)
              FROM     revaluation
              WHERE     curr1     = od.curr
              AND     dt     <= od.dt
              )          AS r_dt
              SELECT     AVG (rate) KEEP (DENSE_RANK LAST ORDER BY dt)
              FROM     revaluation
              WHERE     curr1     = od.curr
              AND     dt     <= od.dt
              )          AS r_rate
         FROM     original_data     od
    SELECT     c.curr
    ,     c.dt
    ,     c.amt
    ,     c.rate
    ,     SUM (p.amt)          AS sumover
    ,     SUM     ( p.amt
              * CASE
                   WHEN     p.dt <= c.r_dt
                   THEN     c.r_rate
                   ELSE     p.rate
                END
         / SUM (p.amt)          AS avgrt
    FROM     two_rates     c
    JOIN     original_data     p     ON     c.curr     = p.curr
                        AND     c.dt     >= p.dt
    GROUP BY     c.curr,     c.dt,     c.amt,     c.rate
    ORDER BY     c.curr,     c.dt
    ;

  • Analytical function syntax help

    The following query sorts by most occuring hesid descedning and requires TWO full table scans of the episodes_full_test table :
    create table episodes_full_test (epikey number, hesid number, dob date) tablespace analysis_ip_d;
    insert into episodes_full_test values (100, 20, to_date('31-07-1975','DD-MM-YYYY'));
    insert into episodes_full_test values (101, 20, to_date('31-07-1975','DD-MM-YYYY'));
    insert into episodes_full_test values (102, 20, to_date('31-07-1975','DD-MM-YYYY'));
    insert into episodes_full_test values (103, 20, to_date('31-07-1975','DD-MM-YYYY'));
    insert into episodes_full_test values (104, 10, to_date('31-07-1985','DD-MM-YYYY'));
    insert into episodes_full_test values (105, 30, to_date('31-07-1995','DD-MM-YYYY'));
    insert into episodes_full_test values (106, 30, to_date('31-07-1995','DD-MM-YYYY'));
    insert into episodes_full_test values (107, 30, to_date('31-07-1995','DD-MM-YYYY'));
    commit;
    select eft.hesid, eft.epikey, eft.dob
    from episodes_full_test eft
    join (select hesid, count(hesid) count_hesid
    from episodes_full_test
    group by hesid) v1
    on eft.hesid = v1.hesid
    order by v1.count_hesid desc, eft.epikey;     
    HESID EPIKEY DOB
    20 100 31/07/1975
    20 101 31/07/1975
    20 102 31/07/1975
    20 103 31/07/1975
    30 105 31/07/1995
    30 106 31/07/1995
    30 107 31/07/1995
    10 104 31/07/1985
    I'm sure there's a way to use analytical functions such that Oracle only needs to perform ONE full table scan of episodes_full_test, but I can't figure out the syntax
    Can anyone advise please ?
    (Oracle 9r2)
    Thanks, Gus

    Thank you for providing the create table, insert commands and required output as it makes answering the question much easier (once I'd removed the tablespace specification)
    SQL> select hesid, epikey, dob
      2  from
      3      (
      4      select eft.hesid, eft.epikey, eft.dob,
      5          count(*) over (partition by eft.hesid) count_hesid
      6      from episodes_full_test eft
      7      )
      8  order by count_hesid desc;
         HESID     EPIKEY DOB
            20        100 31-JUL-75
            20        101 31-JUL-75
            20        102 31-JUL-75
            20        103 31-JUL-75
            30        105 31-JUL-95
            30        106 31-JUL-95
            30        107 31-JUL-95
            10        104 31-JUL-85
    8 rows selected.
    SQL>

  • Return multiple columns from an analytic function with a window

    Hello,
    Is it possible to obtain multiple columns from an analytic function with a window?
    I have a table with 4 columns, an id, a test id, a date, and the result of a test. I'm using an analytic function to obtain, for each row, the current test value, and the maximum test value in the next 2 days like so:
    select
    id,
    test_id,
    date,
    result,
    MAX ( result ) over ( partition BY id, test_id order by date RANGE BETWEEN CURRENT ROW AND INTERVAL '2' DAY FOLLOWING ) AS max_result_next_two_day
    from table
    This is working fine, but I would like to also obtain the date when the max result occurs. I can see that this would be possible using a self join, but I'd like to know if there is a better way? I cannot use the FIRST_VALUE aggregate function and order by result, because the window function needs to be ordered by the date.
    It would be a great help if you could provide any pointers/suggestions.
    Thanks,
    Dan
    http://danieljamesscott.org

    Assuming RESULT is a positive integer that has a maximum width of, say 10,
    and assuming date has no time-component:
    select
       id
      ,test_id
      ,date
      ,result
      ,to_number(substr(max_result_with_date,1,10)) as max_result_next_two_day
      ,to_date(substr(max_result_with_date,11),'YYYYMMDD') as date_where_max_result_occurs
    from (select
            id
           ,test_id
           ,date
           ,result
           ,MAX(lpad(to_char(result),10,'0')||to_char(date,'YYYYMMDD'))
               over (partition BY id, test_id
                     order by date
                     RANGE BETWEEN CURRENT ROW AND INTERVAL '2' DAY FOLLOWING )
            AS max_result_with_date
          from table)

Maybe you are looking for

  • Consuming web service in PHP

    Hello, I have a PHP page that consumes the web service from ABAP side. ABAP is definitely returning a table with a list of materials to the PHP page because we have set a remote breakpoint in the ABAP system and it is breaking at that particular poin

  • Option to manually order the Unplayed Episodes list

    I really like the new podcast app. However, when I am listening to podcasts I don't necessarily want to listen from newest episode to oldest episode in the Unplayed Episodes list. I would like the option to manually order this list. This way I can sp

  • Looping through visa resources

    I have a PXI Chassis that contains 8 serial cards.  For simplicity sake, say they're wired 1-2, 3-4, 5-6, 7-8.  So, basically, what ever I write to 1, I want to read at 2 - the same with the others.  I have the code that configures the com ports and

  • InterMedia Code Samples Error

    Hi, I'm trying to install "Oracle8i interMedia Text 8.1.x Code Samples" and I'm experiencing problems when I run the spider. The spider.pl files contains the following lines: # open the parameter file 'params' open(PARAMS, "params") | | print STDERR

  • Crazy problem with X-Fi ExtremeMusic. Desperate for He

    I have done some research on this problem over the forums and haven't quite found the answer. I have an Asus P5GDC Deluxe Mobo, P4 3.2 Ghz, GB DDR2 memory, Win XP sp2 with all updates. I have disabled the onboard sound card. After inserting the X-Fi