Need analytic function suggestion

Hi,
I need advice related to analytic ( I think ) function in Oracle 9.
create table testx ( id number, arr number, fore number, actual number, result_x number, is_first number);
insert into testx values ( 1, null, null, 12, null , 0 );
insert into testx values ( 2, null, null, 14 , null, 0 );
insert into testx values ( 3, 4, 5, 16, 16, 1 );
insert into testx values ( 4, 5, 5, 18, 16, 0 );
insert into testx values ( 5, 5, 5, 20, 16, 0 );
insert into testx values ( 6, 5, 5, 22, 16, 0 );
insert into testx values ( 7, 5, 5, 24, 16, 0 );
insert into testx values ( 8, 5, 5, 25, 16, 0 );
insert into testx values ( 9, 5, 8, 25, 13, 0 );
insert into testx values ( 10, 5, 8, 21, 10, 0 );
insert into testx values ( 11, 5, 8, 19, 7, 0 );
insert into testx values ( 12, 5, 8, 18, 4, 0 );
I need ONE level query ( no subqueries ) which will calculate value stored in RESULT_X column.
Rule for calculation is:
1. when arr and fore columns are available first time then result_x = actual ( row with id = 3)
2. in other case result_x = (previous value of result_x + arr - fore )
3. order of records is stored in id column
I have problem with calculating previous value of result_x since it should be available in next row calculation and dependents on other columns values.
Thanks for help,
Regards,
Piotr

Hi, Piotr,
This produces the results you requested:
SELECT       testx.*
,       SUM ( CASE
              WHEN  is_first = 1
              THEN  result_x
              ELSE  arr - fore
             END
           ) OVER (ORDER BY  id)     AS computed_result_x
FROM      testx
ORDER BY  id
;This relies on the fact that there is only one row where is_first=1, and that all the earlier rows have NULL as arr or fore.
If that's not the case in your real data, then I don't think it's possible in SQL without sub-queries. Why can't you use a sub-query?
The problem is that rows up to the one with is_first=1 have to be treated differently from rows after that point, so ithe CASE expression might need to know if a given row is before or after the one with is_first=1. If you need an analytic function to determine that, then you need a sub-query, becuase analytic functions can not be nested.
You could use MODEL or a recursive WITH clause to get the results you want, but they require sub-querries.

Similar Messages

  • Help me on Analytical function

    Hi
    I'm using oracle version as
    SQL> select * From v$version;
    BANNER
    Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
    PL/SQL Release 9.2.0.8.0 - Production
    CORE    9.2.0.8.0       Production
    TNS for Solaris: Version 9.2.0.8.0 - Production
    NLSRTL Version 9.2.0.8.0 - Productioni have a following tables
    CREATE TABLE emp_info(emp_id number,ename varchar2(10),chk_date date);
    CREATE TABLE emp_month(emp_id number,emp_month_date date,chk_amt number(10,2));
    insert into emp_info VALUES (101,'PAUL',to_date('01-MAR-2009','DD-MON-YYYY'));
    insert into emp_info VALUES (102,'JOHN',to_date('01-APR-2009','DD-MON-YYYY'));
    insert into emp_info VALUES (103,'KRIS',to_date('01-MAY-2009','DD-MON-YYYY'));
    insert into emp_month values (101,'01-DEC-2008',1432);
    insert into emp_month values (101,'01-JAN-2009',1412);
    insert into emp_month values (101,'01-FEB-2009',1632);
    insert into emp_month values (101,'01-MAR-2009',1672);
    insert into emp_month values (102,'01-DEC-2008',2678);
    insert into emp_month values (102,'01-JAN-2009',2786);
    insert into emp_month values (102,'01-FEB-2009',2883);
    insert into emp_month values (102,'01-MAR-2009',2653);
    insert into emp_month values (102,'01-APR-2009',2653);
    insert into emp_month values (103,'01-NOV-2008',2343);
    insert into emp_month values (103,'01-DEC-2008',2311);
    insert into emp_month values (103,'01-JAN-2009',3122);
    insert into emp_month values (103,'01-FEB-2009',3412);
    insert into emp_month values (103,'01-MAR-2009',3312);
    insert into emp_month values (103,'01-APR-2009',3315);
    insert into emp_month values (103,'01-MAY-2009',4321);i'm using following QUERY, to get the results. Is it possible to get these same results using ANALYTICAL functions
    or i would appreciate any better solution than this.
    Select e.emp_id,e.ename,e.chk_date,sum(chk_amt) year_amt
    from emp_month em
    ,(select emp_id,ename,chk_date
    from emp_info)e
    where e.emp_id = em.emp_id
    and em.emp_month_date between trunc(e.chk_date,'YY') AND e.chk_date
    group by e.emp_id,e.ename,e.chk_date

    user12212962 wrote:
    i'm using following QUERY, to get the results. Is it possible to get these same results using ANALYTICAL functionsor i would appreciate any better solution than this.
    I have no idea why you would need analytic function. What I do know: there is no need for inline view in your query:
    SQL> Select e.emp_id,e.ename,e.chk_date,sum(chk_amt) year_amt
      2  from emp_month em
      3  ,(select emp_id,ename,chk_date
      4  from emp_info)e
      5  where e.emp_id = em.emp_id
      6  and em.emp_month_date between trunc(e.chk_date,'YY') AND e.chk_date
      7  group by e.emp_id,e.ename,e.chk_date
      8 
    SQL> /
        EMP_ID ENAME      CHK_DATE    YEAR_AMT
           102 JOHN       01-APR-09      10975
           101 PAUL       01-MAR-09       4716
           103 KRIS       01-MAY-09      17482
    SQL> Select e.emp_id,e.ename,e.chk_date,sum(chk_amt) year_amt
      2  from emp_month em,
      3       emp_info e
      4  where e.emp_id = em.emp_id
      5  and em.emp_month_date between trunc(e.chk_date,'YY') AND e.chk_date
      6  group by e.emp_id,e.ename,e.chk_date
      7  /
        EMP_ID ENAME      CHK_DATE    YEAR_AMT
           102 JOHN       01-APR-09      10975
           101 PAUL       01-MAR-09       4716
           103 KRIS       01-MAY-09      17482
    SQL> SY.

  • Need some kind of Analytical Function

    Hi Oracle experts
    I need a little help from you experts. I have a PARTY table as listed below
    The existing data
    Party key     ID_INTERNAL     EID          BID
    1          11111          123
    1          11111          321
    1          22222          321          899
    1          66666          ------          888
    New records comes
    I have to assign a party key to each record based on which attribute is matching
    Now the situation is as new records comes.
    New records comes
    ID_INTERNAL     EID          BID
    22222          555
    44444          555          
    89898          ------          888
    If I match on ID_INTERNAL I may not be able to match ID_INTERNAL 44444 and 89898 and if I match EID or BID the same situation.
    Is thera any analytical function which helps me assigning a party key to all the recoords. ALl the above records should be assigned PARTY KEY 1 only.
    Please help
    Thanks
    Rajesh

    Justin
    My main goal is to assign a party key from existing set of records to the new records which are being selected/inserted. I have to write my algoritum in such a way that the new values should match their value in existing records.
    Example
    my first new record has a value of 11111 under ID_INTERNAL and in the same record it has a value of 555 under EID attribute. so based on matching algoritum for ID INTERNAL it will be assigned existing party key 1.
    Similarly second new record has a value of 87777 under ID INTERNAL and has a value of 555 under EID and this ID INTERNAL does not exists in the target table. but the value of 555 is available under EID attribute so I have to write algoritum based on EID.
    Now the delima is my target table is as follows
    Party key PARTYID PARTYNAME
    1 11111 ITSID
    1 123 EID
    1 321 EID
    Now when new records come I have to write match algortium for ID_INTERNAL to PARTYID for Partyname='ITSID'
    Once matched this record ID INTERNAL=11111 and EID =555 assigned a party key=1. So after first record the output table slooks like
    Party key PARTYID PARTYNAME
    1 11111 ITSID
    1 123 EID
    1 321 EID
    1 555 EID
    Same way for second new record where the values are ID_INTERNAL=87777 and EID=555. I have to write match algortium based on EID because the EID value of 555 already exists in target tabel with party key.
    SO after second record the target table will look like
    Party key PARTYID PARTYNAME
    1 11111 ITSID
    1 123 EID
    1 321 EID
    1 555 EID
    1 87777 ITSID
    So this is how I have to solve this match algoritum.
    Please help me if you need any information I will be glad to provide you all.
    Thanks
    Regards
    Rajesh

  • Help needed with analytical function

    I want to get the employee details of the highest and 2nd highest salaried employee in a particular department. But also the department should have more than 1 employee.
    I tried the query and it gave me proper results. But I'm wondering if there is some other alternative than using the subquery.
    Here is the table and the result query :
    with t as
    select 1 emp_id,3 mgr_id,'Rajesh' emp_name,3999 salary,677 bonus,'HR' dpt_nme from dual union
    select 2 ,3 ,'Gangz',4500,800,'Finance' from dual  union
    select 3 ,4 ,'Sid',8000,12000,'IT' from dual  union
    select 4 ,null,'Ram',5000,677,'HR' from dual  union
    select 5 ,4,'Shyam',6000,677,'IT' from dual union
    select 6 ,4 ,'Ravi',9000,12000,'IT' from dual  
    select * from
    (select emp_id, mgr_id, emp_name, dpt_nme, salary, row_number() over (partition by dpt_nme order by salary desc) rn from t where dpt_nme in
    (select dpt_nme from t group by dpt_nme having count(*) > 1)) where rn < 3

    Hi,
    You need a sub-query, but you don't need more than that.
    Here's one way to eliminate the extra sub-query:
    WITH     got_analytics     AS
         SELECT  emp_id,     mgr_id,     emp_name, dpt_nme, salary
         ,     ROW_NUMBER () OVER ( PARTITION BY  dpt_nme
                                   ORDER BY          salary     DESC
                           )         AS rn
         ,     COUNT (*)     OVER ( PARTITION BY  dpt_nme
                                       )         AS dpt_cnt
         FROM     t
    SELECT  emp_id,     mgr_id,     emp_name, dpt_nme, salary
    ,     rn
    FROM     got_analytics
    WHERE     rn     < 3
    AND     dpt_cnt     > 1
    ;Analytic functions are computed after the WHERE clause is applied. Since we need to use the results of the analytic ROW_NUMBER function in a WHERE clause, that means we'll have to compute ROW_NUMBER in a sub-query, and use the results in the WHERE clause of the main query. We can call the analytic COUNT function in the same sub-query, and use its results in the same WHERE clause of the main query.
    What results would you want if there's a tie for the 2nd highest salary in some department? For example, if you add this row to your sample data:
    select 7 ,3 ,'Sunil',8000,12000,'IT' from dual  union? You may want to use RANK instead of ROW_NUMBER.

  • I need help with Analytic Function

    Hi,
    I have this little problem that I need help with.
    My datafile has thousands of records that look like...
    Client_Id Region Countries
    [1] [1] [USA, Canada]
    [1] [2] [Australia, France, Germany]
    [1] [3] [China, India, Korea]
    [1] [4] [Brazil, Mexico]
    [8] [1] [USA, Canada]
    [9] [1] [USA, Canada]
    [9] [4] [Argentina, Brazil]
    [13] [1] [USA, Canada]
    [15] [1] [USA]
    [15] [4] [Argentina, Brazil]
    etc
    My task is is to create a report with 2 columns - Client_Id and Countries, to look something like...
    Client_Id Countries
    [1] [USA, Canada, Australia, France, Germany, China, India, Korea, Brazil, Mexico]
    [8] [USA, Canada]
    [9] [USA, Canada, Argentina, Brazil]
    [13] [USA, Canada]
    [15] [USA, Argentina, Brazil]
    etc.
    How can I achieve this using Analytic Function(s)?
    Thanks.
    BDF

    Hi,
    That's called String Aggregation , and the following site shows many ways to do it:
    http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php
    Which one should you use? That depends on which version of Oracle you're using, and your exact requirements.
    For example, is order importatn? You said the results shoudl include:
    CLIENT_ID  COUNTRIES
    1        USA, Canada, Australia, France, Germany, China, India, Korea, Brazil, Mexicobut would you be equally happy with
    CLIENT_ID  COUNTRIES
    1        Australia, France, Germany, China, India, Korea, Brazil, Mexico, USA, Canadaor
    CLIENT_ID  COUNTRIES
    1        Australia, France, Germany, USA, Canada, Brazil, Mexico, China, India, Korea?
    Mwalimu wrote:
    ... How can I achieve this using Analytic Function(s)?The best solution may not involve analytic functions at all. Is that okay?
    If you'd like help, post your best attempt, a little sample data (CREATE TABLE and INSERT statements), the results you want from that data, and an explanation of how you get those results from that data.
    Always say which version of Oracle you're using.
    Edited by: Frank Kulash on Aug 29, 2011 3:05 PM

  • Analytic Functions - Need resultset only in one select

    Hello Experts,
    Problem Definition: Using Analytic Function, get Total sales for the Product P1 and Customer C1 [Total sales for the customer itself] in one line. I want to restrict the ResultSet of the query to Product P1, please look at the data below, queries and problems..
    Data
    Customer Product Qtr Sales
    C1 P1 19991 100.00
    C1 P1 19992 125.00
    C1 P1 19993 175.00
    C1 P1 19994 300.00
    C1 P2 19991 100.00
    C1 P2 19992 125.00
    C1 P2 19993 175.00
    C1 P2 19994 300.00
    C2 P1 19991 100.00
    C2 P1 19992 125.00
    C2 P1 19993 175.00
    C2 P1 19994 300.00
    Problem, I want to display....
    Customer Product ProdSales CustSales
    C1 P1 700 1400
    But Without using outer query, i.e. please look below for the query that returns this reult with two select, I want this result in one query only..
    Select * From ----*** want to avoid this... ***----
    (Select Customer,Product,
    Sum(Sales) ProdSales,
    Sum(Sum(Sales)) Over(Partition By Customer) CustSales
    From t1
    Where customer='C1')
    Where
    Product='P1' ;
    Also, I want to avoid Hard coding of P1 in the select clause....
    I mean, I can do it in one shot/select, but look at the query below, it uses P1 in the select clause, which is No No!! P1 is allowed only in Where or Having ..
    Select Customer,Decode(Product, 'P1','P1','P1') Product,
    Decode(Product,'P1',Sales,0) ProdSales,
    Sum(Sum(Sales)) Over (Partition By Customer ) CustSales
    From t1
    Where customer='C1' ;
    This will get me what I want, but as I said earlier, I want to avoid using P1 in the
    Select clause..
    Goal is to Avoid using
    1-> Two Select/Outer Query/In Line Views
    2-> Product 'P1' in the Select clause...No hard coded product name in the select clause and group by clause..
    Thanks
    -Dhaval

    Select * From ----*** want to avoid this... ***----
    (Select Customer,Product,
    Sum(Sales) ProdSales,
    Sum(Sum(Sales)) Over(Partition By Customer)
    CustSales
    From t1
    Where customer='C1')
    Where
    Product='P1' ;
    Goal is to Avoid using
    1-> Two Select/Outer Query/In Line ViewsWhy?

  • Need valuable guidance to make a peformance oriented query, trying to replace unions with analytical function

    Hi,
       Please find below table structure and insert scritps. Requesting for vluable help.
    create table temp2 (col1 number,col2 varchar2(10),col3 number,col4 varchar2(20));
    insert into temp2 values (1,'a',100,'vvv');
    insert into temp2 values (2,'b',200,'www'); 
    insert into temp2 values (3,'c',300,'xxx');
    insert into temp2 values (4,'d',400,'yyy');   
    insert into temp2 values (5,'e',500,'zzz');
    insert into temp2 values (6,'f',600,'aaa');
    insert into temp2 values (7,'g',700,'bbb'); 
    insert into temp2 values (8,'h',800,'ccc');
    I am trying to get same output, what we get from below UNION query with ANALYTICAL Function.
    select * from temp2 where col1 in (1,2,3,4,5)
    union
    select * from temp2 where col1 in (1,2,5,6)
    union
    select * from temp2 where col1 in (1,2,7,8);
    I am seeking help by this dummy example to understand the concept, how can we use analytical functional over UNION or OUTER JOINS.
    In my exact query, I am using same table three times adding UNION clause. here also we scan temp2 three times, so for bulky tables using 'union'  would be hampering query's performance
    It means i go with three time scans of same table that is not performance oriented. With the help of above required concept, i will try to remove UNIONs from my exact query.
    Thanks!!

    Thanks for your time BluShadow and sorry as i think i couldn't make my query clear.
    I try it again. Below there are three queries, you may see all three queries are using same tables. Difference in all three queries are just few conditions, which makes all three queries diff with each other.
    I know, u cant run below query in your database, but i think it will convey my doubt to you. I have mentioned no. of rows with each clause and total i am getting 67 rows as my output. (Reason may be first n third query's result set are the subset of Second Query dataset)
    So i want to take all common rows as well as additional rows, if present in any of the query. This is getting easliy done with UNION clause but want to have it in other way as here my same is getting scanned again n again.
    SELECT
             START_TX.FX_TRAN_ID START_FX_TRAN_ID
            ,END_TX.FX_TRAN_ID END_FX_TRAN_ID
            ,START_TX.ENTERED_DT_TS
            ,USER
            ,START_TX.TRADE_DT
            ,START_TX.DEAL_NUMBER
            ,START_TX.FX_DEAL_TYPE
            ,START_TX.ORIENTATION_BUYSELL
            ,START_TX.BASE_CCY
            ,START_TX.BASE_CCY_AMT
            ,START_TX.SECONDARY_CCY
            ,START_TX.SECONDARY_CCY_AMT
            ,START_TX.MATURITY_DT
            ,START_TX.TRADE_RT
            ,START_TX.FORWARD_PTS              
            ,START_TX.CORPORATE_PIPS           
            ,START_TX.DEAL_OWNER_INITIALS      
            ,START_TX.CORPORATE_DEALER         
            ,START_TX.PROFIT_CENTER_CD
            ,START_TX.COUNTERPARTY_NM
            ,START_TX.COUNTERPARTY_NUMBER
      FROM
          (SELECT * FROM FX_TRANSACTIONS WHERE GMT_CONV_ENTERED_DT_TS >=  TO_DATE('20-Nov-2013 4:00:01 AM','DD-Mon-YYYY HH:MI:SS AM')) START_TX
           INNER JOIN
          (SELECT * FROM FX_TRANSACTIONS WHERE GMT_CONV_ENTERED_DT_TS <=  TO_DATE('20-Nov-2013 4:59:59 PM','DD-Mon-YYYY HH:MI:SS AM'))  END_TX
       ON START_TX.COUNTERPARTY_NM        = END_TX.COUNTERPARTY_NM         AND
          START_TX.COUNTERPARTY_NUMBER    = END_TX.COUNTERPARTY_NUMBER     AND
          START_TX.FX_DEAL_TYPE           = END_TX.FX_DEAL_TYPE            AND
          START_TX.BASE_CCY               = END_TX.BASE_CCY                AND
          START_TX.SECONDARY_CCY          = END_TX.SECONDARY_CCY           AND
          NVL(START_TX.CORPORATE_DEALER,'nullX')=NVL(END_TX.CORPORATE_DEALER,'nullX')       AND
          START_TX.ORIENTATION_BUYSELL='B'                                 AND 
          END_TX.ORIENTATION_BUYSELL='S'                                  AND
          START_TX.FX_TRAN_ID = 1850718                                  AND
         (START_TX.BASE_CCY_AMT           = END_TX.BASE_CCY_AMT          
          OR
          START_TX.SECONDARY_CCY_AMT      = END_TX.SECONDARY_CCY_AMT)        -- 10 Rows
    UNION
    SELECT
             START_TX.FX_TRAN_ID START_FX_TRAN_ID
            ,END_TX.FX_TRAN_ID END_FX_TRAN_ID
            ,START_TX.ENTERED_DT_TS
            ,USER
            ,START_TX.TRADE_DT
            ,START_TX.DEAL_NUMBER
            ,START_TX.FX_DEAL_TYPE
            ,START_TX.ORIENTATION_BUYSELL
            ,START_TX.BASE_CCY
            ,START_TX.BASE_CCY_AMT
            ,START_TX.SECONDARY_CCY
            ,START_TX.SECONDARY_CCY_AMT
            ,START_TX.MATURITY_DT
            ,START_TX.TRADE_RT
            ,START_TX.FORWARD_PTS              
            ,START_TX.CORPORATE_PIPS           
            ,START_TX.DEAL_OWNER_INITIALS      
            ,START_TX.CORPORATE_DEALER         
            ,START_TX.PROFIT_CENTER_CD
            ,START_TX.COUNTERPARTY_NM
            ,START_TX.COUNTERPARTY_NUMBER
      FROM
          (SELECT * FROM FX_TRANSACTIONS WHERE GMT_CONV_ENTERED_DT_TS >=  TO_DATE('20-Nov-2013 4:00:01 AM','DD-Mon-YYYY HH:MI:SS AM')) START_TX
           INNER JOIN
          (SELECT * FROM FX_TRANSACTIONS WHERE GMT_CONV_ENTERED_DT_TS <=  TO_DATE('20-Nov-2013 4:59:59 PM','DD-Mon-YYYY HH:MI:SS AM'))  END_TX
       ON START_TX.COUNTERPARTY_NM        = END_TX.COUNTERPARTY_NM         AND
          START_TX.COUNTERPARTY_NUMBER    = END_TX.COUNTERPARTY_NUMBER     AND
          START_TX.FX_DEAL_TYPE           = END_TX.FX_DEAL_TYPE            AND
          START_TX.BASE_CCY               = END_TX.BASE_CCY                AND
          START_TX.SECONDARY_CCY          = END_TX.SECONDARY_CCY           AND
          NVL(START_TX.CORPORATE_DEALER,'nullX')=NVL(END_TX.CORPORATE_DEALER,'nullX')  AND
          START_TX.FX_TRAN_ID = 1850718                                  AND
          START_TX.ORIENTATION_BUYSELL='B'                                 AND 
          END_TX.ORIENTATION_BUYSELL='S'                        --                                   67 Rows
    UNION 
    SELECT
             START_TX.FX_TRAN_ID START_FX_TRAN_ID
            ,END_TX.FX_TRAN_ID END_FX_TRAN_ID
            ,START_TX.ENTERED_DT_TS
            ,USER
            ,START_TX.TRADE_DT
            ,START_TX.DEAL_NUMBER
            ,START_TX.FX_DEAL_TYPE
            ,START_TX.ORIENTATION_BUYSELL
            ,START_TX.BASE_CCY
            ,START_TX.BASE_CCY_AMT
            ,START_TX.SECONDARY_CCY
            ,START_TX.SECONDARY_CCY_AMT
            ,START_TX.MATURITY_DT
            ,START_TX.TRADE_RT
            ,START_TX.FORWARD_PTS              
            ,START_TX.CORPORATE_PIPS           
            ,START_TX.DEAL_OWNER_INITIALS      
            ,START_TX.CORPORATE_DEALER         
            ,START_TX.PROFIT_CENTER_CD
            ,START_TX.COUNTERPARTY_NM
            ,START_TX.COUNTERPARTY_NUMBER
      FROM
          (SELECT * FROM FX_TRANSACTIONS WHERE GMT_CONV_ENTERED_DT_TS >=  TO_DATE('20-Nov-2013 4:00:01 AM','DD-Mon-YYYY HH:MI:SS AM')) START_TX
           INNER JOIN
          (SELECT * FROM FX_TRANSACTIONS WHERE GMT_CONV_ENTERED_DT_TS <=  TO_DATE('20-Nov-2013 4:59:59 PM','DD-Mon-YYYY HH:MI:SS AM'))  END_TX
       ON START_TX.COUNTERPARTY_NM        = END_TX.COUNTERPARTY_NM         AND
          START_TX.COUNTERPARTY_NUMBER    = END_TX.COUNTERPARTY_NUMBER     AND
          START_TX.FX_DEAL_TYPE           = END_TX.FX_DEAL_TYPE            AND
          START_TX.BASE_CCY               = END_TX.BASE_CCY                AND
          START_TX.SECONDARY_CCY          = END_TX.SECONDARY_CCY           AND
          NVL(START_TX.CORPORATE_DEALER,'nullX')=NVL(END_TX.CORPORATE_DEALER,'nullX') AND
          START_TX.ORIENTATION_BUYSELL='B'                                 AND 
          END_TX.ORIENTATION_BUYSELL='S'                                   AND
          START_TX.FX_TRAN_ID = 1850718                                  AND
            END_TX.BASE_CCY_AMT BETWEEN (START_TX.BASE_CCY_AMT - (START_TX.BASE_CCY_AMT * :PERC_DEV/100)) AND (START_TX.BASE_CCY_AMT + (START_TX.BASE_CCY_AMT * :PERC_DEV/100))        
            OR
            END_TX.SECONDARY_CCY_AMT BETWEEN (START_TX.SECONDARY_CCY_AMT - (START_TX.SECONDARY_CCY_AMT*:PERC_DEV/100) ) AND (START_TX.SECONDARY_CCY_AMT + (START_TX.SECONDARY_CCY_AMT*:PERC_DEV/100))
        );                                                       ---                              10 Rows

  • 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>

  • A Job for 'PARTION BY' Analytical Function?

    Hi,
    I'm still a little fuzzy on using partitions but this looks like a possible candidate to me.
    I need to count the number of different customers that visit an office in a single day. If a customer visits an office more than once in a single day that counts as 1.
    Input
    OFFICE CUSTOMER TRAN_DATE
    1     11     1-Apr-09
    1     11     1-Apr-09
    1     11     1-Apr-09
    1     11     2-Apr-09
    2     22     2-Apr-09
    2     22     2-Apr-09
    2     33     2-Apr-09
    select a.office as "OFFICE", a.customer AS "CUSTOMER", a.tran_date AS "TRAN_DATE", COUNT(*)
    FROM
    (SELECT 1 AS "OFFICE", 11 AS "CUSTOMER", '01-APR-2009' AS "TRAN_DATE" FROM DUAL
    UNION ALL
    SELECT 1 , 11 , '01-APR-2009' FROM DUAL
    UNION ALL
    SELECT 1 , 11 , '01-APR-2009' FROM DUAL
    UNION ALL
    SELECT 1 , 11 , '02-APR-2009' FROM DUAL
    UNION ALL
    SELECT 2 , 22 , '02-APR-2009' FROM DUAL
    UNION ALL
    SELECT 2 , 22 , '02-APR-2009' FROM DUAL
    UNION ALL
    SELECT 2 , 33 , '02-APR-2009' FROM DUAL
    ) a;
    Desired Result
    1     1-Apr-09     1
    1     2-Apr-09     1
    2     2-Apr-09     2
    Is this possible with partitions, do I need to use subqueries, or some other methid?
    Thank You in Advance for Your Help,
    Lou
    Edited by: Wind In Face on Apr 15, 2009 1:34 PM

    "I wanted to use PARTITION BY instead of what John suggested because it is my understanding that PARTION BY will be faster"
    It may be, or it may not be. As Frank pointed out analytic functions have their uses, and aggregate functions have theis. In some places, those uses do overlap, but not always. You query is equivalent to mine, that is, it returns the same resultset in this case, however, there are some differences.
    For the relatively small amount of data I generated, it is probably not significant, but the analytic version does two sorts (one unique) while my aggregate version does only one.
    SQL> CREATE TABLE test (office NUMBER, customer NUMBER, tran_dt DATE);
    Table created.
    SQL> INSERT /*+ APPEND */ INTO test
      2  SELECT MOD(rownum, 10)+1, MOD(rownum, 121)+1, TRUNC(sysdate+MOD(rownum, 42))
      3  FROM all_objects;
    18135 rows created.
    SQL> COMMIT;
    Commit complete.
    SQL> SELECT office, tran_dt, COUNT(DISTINCT customer) cust_count
      2  FROM test
      3  GROUP BY office, tran_dt;
    210 rows selected.
    Execution Plan
    Plan hash value: 2407667464
    | Id  | Operation          | Name |
    |   0 | SELECT STATEMENT   |      |
    |   1 |  SORT GROUP BY     |      |
    |   2 |   TABLE ACCESS FULL| TEST |
    Statistics
              0  recursive calls
              0  db block gets
             27  consistent gets
              0  physical reads
              0  redo size
           6061  bytes sent via SQL*Net to client
            631  bytes received via SQL*Net from client
             15  SQL*Net roundtrips to/from client
              1  sorts (memory)
              0  sorts (disk)
            210  rows processed
    SQL> SELECT DISTINCT office office, tran_dt tran_date,
      2         COUNT(DISTINCT customer) OVER(PARTITION BY office, tran_dt) cust_count
      3  FROM test;
    210 rows selected.
    Execution Plan
    Plan hash value: 1303194651
    | Id  | Operation           | Name |
    |   0 | SELECT STATEMENT    |      |
    |   1 |  SORT UNIQUE        |      |
    |   2 |   WINDOW SORT       |      |
    |   3 |    TABLE ACCESS FULL| TEST |
    Statistics
              0  recursive calls
              0  db block gets
             27  consistent gets
              0  physical reads
              0  redo size
           6063  bytes sent via SQL*Net to client
            631  bytes received via SQL*Net from client
             15  SQL*Net roundtrips to/from client
              2  sorts (memory)
              0  sorts (disk)
            210  rows processedFor a larger resultset, this could make a significant difference.
    In general, my preference is use the simplest construct that will work.
    John

  • 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)

  • 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?

  • Aggregation of analytic functions not allowed

    Hi all, I have a calculated field called Calculation1 with the following calculation:
    AVG(Resolution_time) KEEP(DENSE_RANK FIRST ORDER BY RANK ) OVER(PARTITION BY "User's Groups COMPL".Group Name,"Tickets Report #7 COMPL".Resource Name )
    The result of this calculation is correct, but is repeated for all the rows I have in the dataset.
    Group Name      Resourse name    Calculation1
    SH Group            Mr. A            10
    SH Group            Mr. A            10
    SH Group            Mr. A            10
    SH Group            Mr. A            10
    SH Group            Mr. A            10
    5112 rowsI tried to create another calculation in order to have only ONE value for the couple "Group Name, Resource Name) as AVG(Calculation1) but I have the error: Aggregation of analytic functions not allowed
    I saw also inside the "Edit worksheet" panel that the Calculation1 *is not represented* with the "Sigma" symbol I(as for example a simple AVG(field_1)) and inside the SQL code I don't have GROUP BY Group Name, Resource Name......
    I'd like to see ONLY one row as:
    Group Name      Resourse name    Calculation1
    SH Group            Mr. A            10....that it means I grouped by Group Name, Resource Name
    Anyone knows how can I achieve this result or any workarounds ??
    Thanks in advance
    Alex

    Hi Rod unfortunately I can't use the AVG(Resolution_time) because my dataset is quite strange...I explain to you better.
    Ι start from this situation:
    !http://www.freeimagehosting.net/uploads/6c7bba26bd.jpg!
    There are 3 calculated fields:
    RANK is the first calculated field:
    ROW_NUMBER() OVER(PARTITION BY "User's Groups COMPL".Group Name,"Tickets Report Assigned To & Created By COMPL".Resource Name,"Tickets Report Assigned To & Created By COMPL".Incident Id  ORDER BY  "Tickets Report Assigned To & Created By COMPL".Select Flag )
    RT Calc is the 2nd calculation:
    CASE WHEN RANK = 1 THEN Resolution_time END
    and Calculation2 is the 3rd calculation:
    AVG(Resolution_time) KEEP(DENSE_RANK FIRST ORDER BY  RANK ) OVER(PARTITION BY "User's Groups COMPL".Group Name,"Tickets Report Assigned To & Created By COMPL".Resource Name )
    As you can see, from the initial dataset, I have duplicated incident id and a simple AVG(Resolution Time) counts also all the duplication.
    I used the rank (based on the field "flag) to take, for each ticket, ONLY a "resolution time" value (in my case I need the resolution time when the rank =1)
    So, with the Calculation2 I calculated for each couple Group Name, Resource Name the right AVG(Resolution time), but how yuo can see....this result is duplicated for each incident_id....
    What I need instead is to see *once* for each couple 'Group Name, Resource Name' the AVG(Resolution time).
    In other words I need to calculate the AVG(Resolution time) considering only the values written inside the RT Calc fields (where they are NOT NULL, and so, the total of the tickets it's not 14, but 9).
    I tried to aggregate again using AVG(Calculation2)...but I had the error "Aggregation of analytic functions not allowed"...
    Do you know a way to fix this problem ?
    Thanks
    Alex

  • Analytic function to count rows based on Special criteria

    Hi
    I have the following query with analytic function but wrong results on the last column COUNT.
    Please help me to achive the required result.Need to change the way how I select the last column.
    1)I am getting the output order by b.sequence_no column . This is a must.
    2)COUNT Column :
    I don't want the total count based on thor column hence there is no point in grouping by that column.
    The actual requirement to achieve COUNT is:
    2a -If in the next row, if either the THOR and LOC combination changes to a new value, then COUNT=1
    (In other words, if it is different from the following row)
    2b-If the values of THOR and LOC repeats in the following row, then the count should be the total of all those same value rows until the rows become different.
    (In this case 2b-WHERE THE ROWS ARE SAME- also I only want to show these same rows only once. This is shown in the "MY REQUIRED OUTPUT) .
    My present query:
    select    r.name REGION ,
              p.name PT,
              do.name DELOFF,
              ro.name ROUTE,
    decode(th.thorfare_name,'OSIUNKNOWN',NULL,th.thorfare_name)
               THOR,
             l.name LOC ,
              b.sequence_no SEQ,
               CASE WHEN th.thorfare_name = LAG (th.thorfare_name)
                OVER (order by b.sequence_no)
                or th.thorfare_name = LEAD (th.thorfare_name)
                OVER (order by b.sequence_no)
                THEN  COUNT(b.sequence_no) OVER (partition by r.name,th.thorfare_name,l.name order BY b.sequence_no
              ELSE 1
              END COUNT
    from   t_regions r,t_post_towns p,t_delivery_offices do, t_routes ro, t_counties c,t_head_offices ho,
    t_buildings b,t_thoroughfares th,t_localities l
    where   th.thorfare_id = b.thorfare_id
    and    nvl(b.invalid,'N')='N'
    and    b.route_id=ro.route_id(+)
    and    b.locality_id =l.locality_id(+)
    and    ro.delivery_office_id=do.delivery_office_id(+)
    and    do.post_town_id = p.post_town_id(+)
    and    p.ho_id=ho.ho_id(+)
    and     ho.county_id = c.county_id(+)
    and     c.region_id = r.region_id(+)
    and    r.name='NAAS'
    and    do.DELIVERY_OFFICE_id= &&DELIVERY_OFFICE_id
    and    ro.route_id=3405
    group by r.name,p.name,do.name,ro.name,th.thorfare_name,l.name,b.sequence_no
    ORDER BY ro.name,b.sequence_no;My incorrect output[PART OF DATA]:
    >
    REGION     PT DELOFF ROUTE     THOR LOC SEQ COUNT
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 1 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 2 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 PRIMHILL CEL 4 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 5 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 THEGROVE CEL 2 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 7 3
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 8 4
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 9 5
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 10 6
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 11 7
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 12 8
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 15 2
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 19 3
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 24 4
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 29 5
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 34 6
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 39 7
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 42 2
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 PRIMHILL CEL 43 2
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 PRIMHILL CEL 44 3
    My required output[PART OF DATA]-Please compare with the above.:
    >
    REGION     PT DELOFF ROUTE     THOR LOC COUNT
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 PRIMHILL CEL 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 THEGROVE CEL 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 6
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 7
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 PRIMHILL CEL 2
    NOTE :Count as 1 is correctly coming.
    But where there is same rows and I want to take the total count on them, I am not getting.
    Pls pls help.
    Thanks
    Edited by: Krithi on 04-Nov-2010 05:28

    Nicosa wrote:
    Hi,
    Can you give us some sample data (create table + inserts orders) to play with ?
    Considering your output, I'm not even sure you need analytic count.Yes sure.
    I am describing the query again here with 3 tables now to make this understand better.
    Given below are the create table statements and insert statements for these 3 tables.
    These tables are - BULDINGSV,THORV and LOCV
    CREATE TABLE BUILDINGSV
      BUILDING_ID                  NUMBER(10)       NOT NULL,
      INVALID                      VARCHAR2(1 BYTE),
      ROUTE_ID                     NUMBER(10),
      LOCALITY_ID                  NUMBER(10),
      SEQUENCE_NO                  NUMBER(4),
      THORFARE_ID                  NUMBER(10) NOT NULL
    CREATE TABLE THORV
      THORFARE_ID            NUMBER(10)             NOT NULL,
      THORFARE_NAME          VARCHAR2(40 BYTE)      NOT NULL
    CREATE TABLE LOCV
      LOCALITY_ID            NUMBER(10)             NOT NULL,
      NAME                   VARCHAR2(40 BYTE)      NOT NULL);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002372, 'N', 3405, 37382613, 5, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002363, 'N', 3405, 37382613, 57, 9002364);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002362, 'N', 3405, 37382613, 56, 9002364);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002360, 'N', 3405, 37382613, 52, 9002364);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002358, 'N', 3405, 37382613, 1, 9002364);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002240, 'N', 3405, 37382613, 6, 9002284);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002229, 'N', 3405, 37382613, 66, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002228, 'N', 3405, 37382613, 65, 35291872);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002226, 'N', 3405, 37382613, 62, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002222, 'N', 3405, 37382613, 43, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002217, 'N', 3405, 37382613, 125, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002221, 'N', 3405, 37382613, 58, 9002364);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002214, 'N', 3405, 37382613, 128, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33363182, 'N', 3405, 37382613, 114, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33363185, 'N', 3405, 37382613, 115, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002371, 'N', 3405, 37382613, 2, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003329, 'N', 3405, 37382613, 415, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002359, 'N', 3405, 37382613, 15, 9002364);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002224, 'N', 3405, 37382613, 61, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003318, 'N', 3405, 37382613, 411, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003326, 'N', 3405, 37382613, 412, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003327, 'N', 3405, 37382613, 413, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003328, 'N', 3405, 37382613, 414, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003330, 'N', 3405, 37382613, 416, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003331, 'N', 3405, 37382613, 417, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003332, 'N', 3405, 37382613, 410, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27004795, 'N', 3405, 37382613, 514, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27004807, 'N', 3405, 37382613, 515, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002227, 'N', 3405, 37382613, 64, 35291872);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33230805, 'N', 3405, 37382613, 44, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33231027, 'N', 3405, 37382613, 7, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33231058, 'N', 3405, 37382613, 9, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33231078, 'N', 3405, 37382613, 10, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33231087, 'N', 3405, 37382613, 11, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33231093, 'N', 3405, 37382613, 12, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33229890, 'N', 3405, 37382613, 55, 9002364);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561996, 'N', 3405, 34224751, 544, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561997, 'N', 3405, 34224751, 543, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561998, 'N', 3405, 34224751, 555, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562000, 'N', 3405, 34224751, 541, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562001, 'N', 3405, 34224751, 538, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562028, 'N', 3405, 35417256, 525, 0);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562031, 'N', 3405, 35417256, 518, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562032, 'N', 3405, 35417256, 519, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562033, 'N', 3405, 35417256, 523, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561939, 'N', 3405, 34224751, 551, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561940, 'N', 3405, 34224751, 552, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561941, 'N', 3405, 34224751, 553, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561942, 'N', 3405, 35417256, 536, 0);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561943, 'N', 3405, 35417256, 537, 0);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561970, 'N', 3405, 35417256, 522, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561972, 'N', 3405, 35417256, 527, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561974, 'N', 3405, 35417256, 530, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561975, 'N', 3405, 35417256, 531, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561980, 'N', 3405, 34224751, 575, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561981, 'N', 3405, 34224751, 574, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561983, 'N', 3405, 34224751, 571, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561984, 'N', 3405, 34224751, 570, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561985, 'N', 3405, 34224751, 568, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561986, 'N', 3405, 34224751, 567, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561987, 'N', 3405, 34224751, 566, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561989, 'N', 3405, 34224751, 563, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561990, 'N', 3405, 34224751, 562, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561991, 'N', 3405, 34224751, 560, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561992, 'N', 3405, 34224751, 559, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561993, 'N', 3405, 34224751, 558, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561994, 'N', 3405, 34224751, 548, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561995, 'N', 3405, 34224751, 546, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562160, 'N', 3405, 37382613, 139, 35291878);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562161, 'N', 3405, 37382613, 140, 35291878);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562162, 'N', 3405, 37382613, 141, 35291878);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562163, 'N', 3405, 37382613, 142, 35291878);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562164, 'N', 3405, 37382613, 143, 35291878);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562165, 'N', 3405, 37382613, 145, 35291878);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562166, 'N', 3405, 37382613, 100, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562167, 'N', 3405, 37382613, 102, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562171, 'N', 3405, 37382613, 107, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562172, 'N', 3405, 37382613, 108, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562174, 'N', 3405, 37382613, 110, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562175, 'N', 3405, 37382613, 111, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562176, 'N', 3405, 37382613, 112, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562177, 'N', 3405, 37382613, 113, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562182, 'N', 3405, 37382613, 123, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562183, 'N', 3405, 37382613, 121, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562184, 'N', 3405, 37382613, 120, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562185, 'N', 3405, 37382613, 118, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562186, 'N', 3405, 37382613, 117, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562187, 'N', 3405, 37382613, 116, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562189, 'N', 3405, 37382613, 95, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562190, 'N', 3405, 37382613, 94, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562213, 'N', 3405, 37382613, 89, 35291872);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562240, 'N', 3405, 35417256, 516, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329559, 'N', 3405, 35329152, 443, 35329551);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329560, 'N', 3405, 35329152, 444, 35329551);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329562, 'N', 3405, 35329152, 446, 35329551);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329109, 'N', 3405, 35329152, 433, 35329181);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329169, 'N', 3405, 35329152, 434, 35329181);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329557, 'N', 3405, 35329152, 441, 35329551);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329558, 'N', 3405, 35329152, 442, 35329551);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329191, 'N', 3405, 35329152, 436, 35329181);
    COMMIT;
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (0, 'OSIUNKNOWN');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (9002284, 'THE GROVE');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (9002364, 'DUBLIN ROAD');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (9002375, 'NEWTOWN ROAD');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35291872, 'HAZELHATCH ROAD');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35291878, 'SIMMONSTOWN PARK');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35291883, 'PRIMROSE HILL');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35329181, 'THE COPSE');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35329213, 'THE COURT');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35329529, 'THE CRESCENT');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35329551, 'THE LAWNS');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35329580, 'THE DRIVE');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35417271, 'TEMPLEMILLS COTTAGES');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35417360, 'CHELMSFORD');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (36500023, 'THE CLOSE');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (36500101, 'THE GREEN');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (37375569, 'THE DOWNS');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (37375595, 'THE PARK');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (37375754, 'THE AVENUE');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (37375781, 'THE VIEW');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (37376046, 'THE CRESCENT');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (37376048, 'THE GLADE');
    COMMIT;
    Insert into LOCV
       (LOCALITY_ID, NAME)
    Values
       (34224751, 'SIMMONSTOWN');
    Insert into LOCV
       (LOCALITY_ID, NAME)
    Values
       (35417256, 'TEMPLEMILLS');
    Insert into LOCV
       (LOCALITY_ID, NAME)
    Values
       (35329152, 'TEMPLE MANOR');
    Insert into LOCV
       (LOCALITY_ID, NAME)
    Values
       (37382613, 'CELBRIDGE');
    Insert into LOCV
       (LOCALITY_ID, NAME)
    Values
       (37375570, 'SAINT WOLSTAN''S ABBEY');
    COMMIT;
    ------------------------------------------------------------------------------Now the query with wrong result:
    select decode(th.thorfare_name,'OSIUNKNOWN',NULL,th.thorfare_name)
               THOR,
                l.name LOC,
                b.sequence_no SEQ,
               CASE WHEN th.thorfare_name = LAG (th.thorfare_name)
                OVER (order by b.sequence_no)
                or th.thorfare_name = LEAD (th.thorfare_name)
                OVER (order by b.sequence_no)
                THEN  COUNT(b.sequence_no) OVER (partition by th.thorfare_name,l.name order BY b.sequence_no
              ELSE 1
              END COUNT   from BUILDINGSV b,THORV th,LOCV l                 
    where   th.thorfare_id = b.thorfare_id
    and    nvl(b.invalid,'N')='N'
    and    b.route_id=3405
    and    b.locality_id =l.locality_id(+)
    order by b.sequence_no;The query result -WRONG (only first few lines)
    THOR                        LOC        SEQ    COUNT
    DUBLIN ROAD     CELBRIDGE    1     1
    NEWTOWN ROAD     CELBRIDGE        2     1
    NEWTOWN ROAD     CELBRIDGE        5     2
    THE GROVE     CELBRIDGE        6     1
    NEWTOWN ROAD     CELBRIDGE        7     3
    NEWTOWN ROAD     CELBRIDGE        9     4
    NEWTOWN ROAD     CELBRIDGE       10     5
    NEWTOWN ROAD     CELBRIDGE       11     6
    NEWTOWN ROAD     CELBRIDGE       12     7
    DUBLIN ROAD     CELBRIDGE       15     1
    PRIMROSE HILL     CELBRIDGE       43     1
    PRIMROSE HILL     CELBRIDGE       44     2
    DUBLIN ROAD     CELBRIDGE       52     3
    DUBLIN ROAD     CELBRIDGE       55     4
    DUBLIN ROAD     CELBRIDGE       56     5
    DUBLIN ROAD     CELBRIDGE       57     6
    DUBLIN ROAD     CELBRIDGE       58     7
    PRIMROSE HILL     CELBRIDGE       61     3
    PRIMROSE HILL     CELBRIDGE       62     4
    HAZELHATCH ROAD     CELBRIDGE       64     1
    HAZELHATCH ROAD     CELBRIDGE       65     2The query result -EXPECTED (only first few lines)
    THOR                     LOC     COUNT
    DUBLIN ROAD     CELBRIDGE      1
    NEWTOWN ROAD     CELBRIDGE      2
    THE GROVE     CELBRIDGE      1
    NEWTOWN ROAD     CELBRIDGE      5
    DUBLIN ROAD     CELBRIDGE      1
    PRIMROSE HILL     CELBRIDGE      2
    DUBLIN ROAD     CELBRIDGE      5
    PRIMROSE HILL     CELBRIDGE      2
    HAZELHATCH ROAD     CELBRIDGE      2Please note, in the expected result, I only need 1 row but need to show the total count of rows until the names change.
    So the issues are
    1) the count column values are wrong in my query.
    2)I dont want to repeat the same rows(Please see EXPECTED output and compare it against the original)
    3)Want the output in exactly same way as in EXPECTED OUTPUT as I dont want to group by thor name(Eg. I dont want the count for all DUBLIN ROAD but I want to examine rows for the next one, if THOR/LOC combination is different in next row then COUNT=1 else COUNT=Count of no. of rows for that thor/loc combination until the combination change -So there are same value multiple rows which i need to show it in 1 row with the total count)
    I am explaining below this in more detail!!
    I only need 1 row per same THOR/LOC names coming multiple times but I need the count shown against that 1 row(i.e COUNT= how many rows with same thor/loc combination until THOR/LOC combo changes value).
    Then repeat the process until all rows are finished..
    If there is no multiple row with same THOR/LOC coming in the following row-i.e the following row is a different THOR/LOC combination, then the count for that row is 1.
    Hope this is clear.
    Is this doable?
    Thanks in advance.
    Edited by: Krithi on 04-Nov-2010 07:45
    Edited by: Krithi on 04-Nov-2010 07:45
    Edited by: Krithi on 04-Nov-2010 08:31

  • Case Statement in Analytic Function SUM(n) OVER(PARTITION BY x)

    Hi Guys,
    I have the following SQL that doesn't seem to consider the When clause I am using in the case staement inside the analytic function(SUM). Could somebody let me know why? and suggest the solution?
    Select SUM(Case When (A.Flag = 'B' and B.Status != 'C') Then (NVL(A.Amount_Cr, 0) - (NVL(A.Amount_Dr,0))) Else 0 End) OVER (PARTITION BY A.Period_Year) Annual_amount
         , A.period_year
         , B.status
    , A.Flag
    from A, B, C
    where A.period_year = 2006
    and C.Account = '301010'
    --and B.STATUS != 'C'
    --and A.Flag = 'B'
    and A.Col_x = B.Col_x
    and A.Col_y = C.Col_y
    When I use this SQL, I get
    Annual_Amount Period_Year Status Flag
    5721017.5 --------- 2006 ---------- C -------- B
    5721017.5 --------- 2006 ---------- O -------- B
    5721017.5 --------- 2006 ---------- NULL ----- A
    And when I put the conditions in the where clause, I get
    Annual_Amount Period_Year Status Flag
    5721017.5 ---------- 2006 ---------- O -------- B

    Here are some scripts,
    create table testtable1 ( ColxID number(10), ColyID number(10) , Periodname varchar2(15), Flag varchar2(1), Periodyear number(15), debit number, credit number)
    insert into testtable1 values(1, 1000, 'JAN-06', 'A', 2006, 7555523.71, 7647668)
    insert into testtable1 values(2, 1001, 'FEB-06', 'B', 2006, 112710, 156047)
    insert into testtable1 values(3, 1002, 'MAR-06', 'A', 2006, 200.57, 22376.43)
    insert into testtable1 values(4, 1003, 'APR-06', 'B', 2006, 0, 53846)
    insert into testtable1 values(5, 1004, 'MAY-06', 'A', 2006, 6349227.19, 6650278.03)
    create table testtable2 ( ColxID number(10), Account number(10))
    insert into testtable2 values(1, 300100)
    insert into testtable2 values(2, 300200)
    insert into testtable2 values(3, 300300)
    insert into testtable2 values(4, 300400)
    insert into testtable2 values(5, 300500)
    create table apps.testtable3 ( ColyID number(10), Status varchar2(1))
    insert into testtable3 values(1000, 'C')
    insert into testtable3 values(1001, 'O')
    insert into testtable3 values(1002, 'C')
    My SQL:
    select t1.periodyear
         , SUM(Case When (t1.Flag = 'B' and t3.Status != 'C') Then (NVL(t1.credit, 0) - (NVL(t1.debit,0))) Else 0 End) OVER (PARTITION BY t1.PeriodYear)
         Annual_amount
         , t1.flag
         , t3.status
         , t2.account
    from testtable1 t1, testtable2 t2, testtable3 t3
    where t1.colxid = t2.colxid
    and t1.colyid = t3.colyid(+)
    --and t1.Flag = 'B' and t3.Status != 'C'
    Result:
    PeriodYear ----- AnnualAmount ----- Flag ----- Status ----- Account
    2006 ------------------ 43337 --------------- A ----------- C ---------- 300100
    2006 ------------------ 43337 --------------- B ----------- O ---------- 300200
    2006 ------------------ 43337 --------------- A ----------- C ---------- 300300
    2006 ------------------ 43337 --------------- B ------------ ----------- 300400
    2006 ------------------ 43337 --------------- A ------------ ----------- 300500
    With condition "t1.Flag = 'B' and t3.Status != 'C'" in where clause instead of in Case statement, Result is (which is desired)
    PeriodYear ----- AnnualAmount ----- Flag ----- Status ----- Account
    2006 ------------------ 43337 --------------- B ----------- O ---------- 300200

  • Analytical function in OWB 10.2.0.4.0

    Dear -
    I am trying to implement analytical function in OWB but not sure how to do it. Can anyone help me?
    My SQL query looks like
    select sum (aamtorg),
    sum(sum(aamtorg)) over
    (order by cbssuntgbk, caccgbk, caccroo, ccrytrngbk, creftrl,
    cmgmint, cbasent, cbok, tamtlbl,
    cctygbk, caffgbk, dacggll, dctx
    rows between unbounded preceding and current row) cumulative_amountcctybbl
    from fmbnd_evt
    where cbssuntgbk = 'FM001'
    and caccgbk = '14300000029'
    and caccroo = '9146581'
    and ccrytrngbk = 'AUD'
    and creftrl = '~'
    and cmgmint = '~'
    and cbasent = 'U2725'
    and cbok = '0000'
    and tamtlbl = '~'
    and dacggll between '01aug2011' and '04aug11'
    group by cbssuntgbk, caccgbk, caccroo, ccrytrngbk, creftrl,
    cmgmint, ctrdnbmgint, cbasent, cbok, tamtlbl,
    cctygbk, caffgbk, dacggll, dctx
    I want to implement cumulative_amountcctybb column in the mapping.
    Can anyone help?

    Hi Arun,
    analytical functions don't require GROUP BY clause and that's why you can use an expression operator. You also have a normal SUM (aggregate) function in your query, which requires GROUP BY and can only be implemented using aggregator operator. If I understand your problem correctly, you need to use aggregate SUM with GROUP BY on your data set first, and then use analytical SUM on this set (which is already processed with an aggregate SUM). Your query would look something like this:
    select sum_aamtorg,
    sum(sum_aamtorg) over
    (order by cbssuntgbk, caccgbk, caccroo, ccrytrngbk, creftrl,
    cmgmint, cbasent, cbok, tamtlbl,
    cctygbk, caffgbk, dacggll, dctx
    rows between unbounded preceding and current row) cumulative_amountcctybbl
    from (
    select sum (aamtorg) sum_aamtorg,
    cbssuntgbk, caccgbk, caccroo, ccrytrngbk, creftrl,
    cmgmint, cbasent, cbok, tamtlbl,
    cctygbk, caffgbk, dacggll, dctx
    from fmbnd_evt
    where cbssuntgbk = 'FM001'
    and caccgbk = '14300000029'
    and caccroo = '9146581'
    and ccrytrngbk = 'AUD'
    and creftrl = '~'
    and cmgmint = '~'
    and cbasent = 'U2725'
    and cbok = '0000'
    and tamtlbl = '~'
    and dacggll between '01aug2011' and '04aug11'
    group by cbssuntgbk, caccgbk, caccroo, ccrytrngbk, creftrl,
    cmgmint, ctrdnbmgint, cbasent, cbok, tamtlbl,
    cctygbk, caffgbk, dacggll, dctx)
    Operator sequence would then look like: TABLE -> FILTER -> AGGREGATOR ->EXPRESSION.
    Hope this helps
    Mate
    Edited by: mate on Sep 26, 2011 1:36 PM
    Edited by: mate on Sep 26, 2011 1:36 PM

Maybe you are looking for

  • Incoming Emails with Blank message body in Outlook 2013 and 2010

    Dear TechNet Why Some of the incoming message we received in Outlook  are displayed with blank message body but same message when viewed from Internet browser web  client  or in Blackberry device message body is viewable and also same message is view

  • Using AGG/CALC DIM function in the BSO target cube of Transparent Partition

    Hi, Is it possible to use AGG("dimension") on a dimnesion in the BSO cube that is a target cube for a BSO-BSO Transparent Partition/ I have created the partition area with 3 dimensions, one being 'Source'. 90% of the source members are being refrence

  • HR Data Transfer Toolbox - Initial Conversion

    Our project is trying to use the HR Data Transfer Toolbox to initially load all employees using best practice configured action 'X'.  However, we have turned on concurrent employment in our config settings.  This is causing a BDC error.  The BDC reco

  • Printer Profile will not Load

    Hi, I am using Windows XP Pro. I have Lightroom version 1.3.1 and I just set up an Epson R2400 printer and loaded the Epson profiles. In LR's Print module when I select Print Job > Profile > Other I can select my printer profile for the paper I want

  • Itunes Install Crashes when it gets to Registering Modules status

    Appreciate any help someone could offer me on this. I've had the same thing happen several times. Every time I try to install the new version it gets stuck at this status. I can't figure what the problem is. I've gone to the Microsoft update page to