Analytic function sql help

Table with 2 columns pro_id,sub_ver_id. Need only 5 pro_id for each sub_ver_id
SQL> select * from test1 order by SUB_VER_ID;
PRO_ID SUB_VER_ID
1 0
2 0
3 0
4 0
5 0
6 0
10 1
15 1
16 1
11 1
12 1
PRO_ID SUB_VER_ID
13 1
14 1
11 2
12 3
I'm new to analytic function i got query as below, but not able to get an idea to limit the SRLNO to only 5 rows for each SUB_VER_ID. Any hint would be much appreciated.
select distinct sub_ver_id,pro_id, row_number () over (order by sub_ver_id) srlno
from test1 order by sub_ver_id

May be as below..
select *
from
select sub_ver_id,pro_id, row_number () over (partition by sub_ver_id order by null) srlno
from test1
) where srlno <=5 order by sub_ver_idThanks...

Similar Messages

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

  • How to achive this using analytical function-- please help

    version 10g.
    this code works just fine with my requirement. i am tyring to learn analytical functions and implement that in the below query. i tried using row_number ,
    but i could nt achive the desired results. please give me some ideas.
    SELECT c.tax_idntfctn_nmbr irs_number, c.legal_name irs_name,
           f.prvdr_lctn_iid
      FROM tax_entity_detail c,
           provider_detail e,
           provider_location f,
           provider_location_detail pld
    WHERE c.tax_entity_sid = e.tax_entity_sid
       AND e.prvdr_sid = f.prvdr_sid
       AND pld.prvdr_lctn_iid = f.prvdr_lctn_iid
       AND c.oprtnl_flag = 'A'
       AND c.status_cid = 2
       AND e.oprtnl_flag = 'A'
       AND e.status_cid = 2
       AND (c.from_date) =
              (SELECT MAX (c1.from_date)
                 FROM tax_entity_detail c1
                WHERE c1.tax_entity_sid = c.tax_entity_sid
                  AND c1.oprtnl_flag = 'A'
                  AND c1.status_cid = 2)
       AND (e.from_date) =
              (SELECT MAX (c1.from_date)
                 FROM provider_detail c1
                WHERE c1.prvdr_sid = e.prvdr_sid
                  AND c1.oprtnl_flag = 'A'
                  AND c1.status_cid = 2)
       AND pld.oprtnl_flag = 'A'
       AND pld.status_cid = 2
       AND (pld.from_date) =
              (SELECT MAX (a1.from_date)
                 FROM provider_location_detail a1
                WHERE a1.prvdr_lctn_iid = pld.prvdr_lctn_iid
                  AND a1.oprtnl_flag = 'A'
                  AND a1.status_cid = 2)thanks
    Edited by: new learner on May 24, 2010 7:53 AM
    Edited by: new learner on May 24, 2010 10:50 AM

    May be like this not tested...
    select *
    from
    SELECT c.tax_idntfctn_nmbr irs_number, c.legal_name irs_name,
    f.prvdr_lctn_iid, c.from_date as c_from_date, max(c.from_date) over(partition by c.tax_entity_sid) as max_c_from_date,
    e.from_date as e_from_date, max(e.from_date) over(partition by e.prvdr_sid) as max_e_from_date,
    pld.from_date as pld_from_date, max(pld.from_date) over(partition by pld.prvdr_lctn_iid) as max_pld_from_date
    FROM tax_entity_detail c,
    provider_detail e,
    provider_location f,
    provider_location_detail pld
    WHERE c.tax_entity_sid = e.tax_entity_sid
    AND e.prvdr_sid = f.prvdr_sid
    AND pld.prvdr_lctn_iid = f.prvdr_lctn_iid
    AND c.oprtnl_flag = 'A'
    AND c.status_cid = 2
    AND e.oprtnl_flag = 'A'
    AND e.status_cid = 2
    AND pld.oprtnl_flag = 'A'
    AND pld.status_cid = 2
    )X
    where c_from_date=max_c_from_date AND e_from_date =max_e_from_date AND
    pld_from_date=max_pld_from_date

  • How to achive using analytical functions -- please help

    oracle version : 10g
    -- 999 means ALL values for that key
    with input_parameters as (select 10 filter_key , '10ACCC' filter_value from dual union all
    select 50 filter_key ,'10ACCC0001' filter_value from dual union all
    select 60 filter_key , 'PIP' filter_value from dual union all
    select 70 filter_key , 'A' filter_value from dual) select * from input_parameters;
    with profile_search as(
    select 100 profile_id , 10 filter_key , '10ACCC' filter_value from dual union all
    select 100 profile_id , 50 filter_key , '10ACCC0001' filter_value from dual union all
    select 100 profile_id , 50 filter_key , '10ACCC0002' filter_value from dual union all
    select 100 profile_id , 60 filter_key , '999' filter_value from dual union all
    select 100 profile_id , 70 filter_key , '999' filter_value from dual union all
    select 101 profile_id , 10 filter_key , '10ACCC' filter_value from dual union all
    select 101 profile_id , 50 filter_key , '10ACCC001' filter_value from dual union all
    select 101 profile_id , 60 filter_key , 'PIP' filter_value from dual union all
    select 101 profile_id , 70 filter_key , '999' filter_value from dual union all
    select 102 profile_id , 10 filter_key , '10ACCC' filter_value from dual union all
    select 102 profile_id , 50 filter_key , '10ACCC0001' filter_value from dual union all
    select 102 profile_id , 60 filter_key , 'PIP' filter_value from dual union all
    select 102 profile_id , 70 filter_key , 'A' filter_value from dual)
    select filter_key , wm_concat(filter_value) from profile_search group by profile_id, filter_key ;
    need to identify profile that matches input parameters
    102 is first match because it matches exactly
    101 is second match because 999 can match any value
    100 is third match KEY 70 HAS HIGHEST WEIGHT,
    KEY 60 has next highest weight and KEY 50 has next highest weight
    results required :
    profile_id : 102
    Edited by: devarade on Jan 19, 2010 8:01 PM
    Edited by: devarade on Jan 19, 2010 8:01 PM

    I assume there is a typo in your sample:
    select 101 profile_id , 50 filter_key , '10ACCC001' filter_value from dual union allshould be:
    select 101 profile_id , 50 filter_key , '10ACCC00<font color=red size=2>0</font>1' filter_value from dual union allThen:
    with input_parameters as (
                              select 10 filter_key , '10ACCC' filter_value from dual union all
                              select 50 filter_key ,'10ACCC0001' filter_value from dual union all
                              select 60 filter_key , 'PIP' filter_value from dual union all
                              select 70 filter_key , 'A' filter_value from dual
            profile_search as(
                              select 100 profile_id , 10 filter_key , '10ACCC' filter_value from dual union all
                              select 100 profile_id , 50 filter_key , '10ACCC0001' filter_value from dual union all
                              select 100 profile_id , 50 filter_key , '10ACCC0002' filter_value from dual union all
                              select 100 profile_id , 60 filter_key , '999' filter_value from dual union all
                              select 100 profile_id , 70 filter_key , '999' filter_value from dual union all
                              select 101 profile_id , 10 filter_key , '10ACCC' filter_value from dual union all
                              select 101 profile_id , 50 filter_key , '10ACCC0001' filter_value from dual union all
                              select 101 profile_id , 60 filter_key , 'PIP' filter_value from dual union all
                              select 101 profile_id , 70 filter_key , '999' filter_value from dual union all
                              select 102 profile_id , 10 filter_key , '10ACCC' filter_value from dual union all
                              select 102 profile_id , 50 filter_key , '10ACCC0001' filter_value from dual union all
                              select 102 profile_id , 60 filter_key , 'PIP' filter_value from dual union all
                              select 102 profile_id , 70 filter_key , 'A' filter_value from dual
    select  profile_id,
            sum(direct_match_cnt) || ' direct matches out of ' || (select count(*) from input_parameters) match
      from  (
             select  b.profile_id,
                     b.filter_key,
                     b.filter_value,
                     sum(case when b.filter_value = a.filter_value then 1 else 0 end) direct_match_cnt
               from  input_parameters a,
                     profile_search b
               where b.filter_key = a.filter_key
                 and (b.filter_value = a.filter_value or b.filter_value = '999')
               group by b.profile_id,
                        b.filter_key,
                        b.filter_value
            ) x
      group by profile_id
      having count(*) = (select count(*) from input_parameters)
      order by sum(direct_match_cnt) desc
    PROFILE_ID MATCH
           102 4 direct matches out of 4
           101 3 direct matches out of 4
           100 2 direct matches out of 4
    SQL> SY.

  • Are analytic functions usefull only for data warehouses?

    Hi,
    I deal with reporting queries on Oracle databases but I don't work on Data Warehouses, thus I'd like to know if learning to use analytic functions (sql for anaylis such as rollup, cube, grouping, ...) might be usefull in helping me to develop better reports or if analytic functions are usually usefull only for data warehouses queries. I mean are rollup, cube, grouping, ... usefull also on operational database or do they make sense only on DWH?
    Thanks!

    Mark1970 wrote:
    thus does it worth learning them for improving report queries also not on DHW but on common operational databases?Why pigeonhole report queries as "+operational+" or "+data warehouse+"?
    Do you tell a user/manager that "<i>No, this report cannot be done as it looks like a data warehouse report and we have an operational database!</i>"?
    Data processing and data reporting requirements not not care what label you assign to your database.
    Simple real world example of using analytical queries on a non warehouse. We supply data to an external system via XML. They require that we limit the number of parent entities per XML file we supply. E.g. 100 customer elements (together with all their child elements) per file. Analytical SQL enables this to be done by creating "buckets" that can only contain 100 parent elements at a time. Complete process is SQL driven - no slow-by-slow row by row processing in PL/SQL using nested cursor loops and silly approaches like that.
    Analytical SQL is a tool in the developer toolbox. It would be unwise to remove it from the toolbox, thinking that it is not applicable and won't be needed for the work that's to be done.

  • Disco -- Any analytical functions for comparisons

    Hi:
    I'm wondering if there are any analytical function to help me out with comparisons? Users often need to displays totals based on date ranges, and show the difference between the two totals, as well as percent change.
    For example, a workbook would show the comparison of cases and dollars, for 2002 vs. 2003. Currently, my solution for this is to create DECODE calculations based on year and type (cases or dollars), and perform the
    comparisons in separate calculations. I'd like to know if Discoverer already has a function that would handle some of this, and reduce the number of DECODES and separate calculations the users have to create...
    Thanks,
    Subramanyam
    Sr. Technical Consultant
    Oracle Direct

    Hello
    You can use the Oracle database SQL analytic function to perform comparison and window based calculations. Example: LAG, LEAD, RANK, etc...
    Please consult the Oracle 9.2 database documentation as well as Discoverer documentation for examples and syntax.
    Regards
    Discoverer Product Management

  • 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

  • Does sql analytic function help to determine continuity in occurences

    We need to solve this problem in a sql statement.
    imagine a table test with two columns
    create table test (id char(1), begin number, end number);
    and these values
    insert into test('a',1, 2);
    insert into test('a',2,3);
    insert into test('a',3,4);
    insert into test('a',7,10);
    insert into test('a',10,15);
    insert into test('b',5,9);
    insert into test('b',9,21);
    insert into test('c',1,5);
    our goal is to determine continuity in number sequence between begin and end attributes for a same id and determine min and max number from these contuinity chains.
    The result may be
    a, 1, 4
    a, 7, 15
    b, 5, 21
    c, 1, 5
    We test some analytic functions like lag, lead, row_number, min, max, partition by, etc to search a way to identify row set that represent a continuity but we didn't find a way to identify (mark) them so we can use min and max functions to extract extreme values.
    Any idea is really welcome !

    Here is our implementation in a real context for example:
    insert into requesterstage(requesterstage_i, requester_i, t_requesterstage_i, datefrom, dateto )
    With ListToAdd as
    (Select distinct support.requester_i,
    support.datefrom,
    support.dateto
    from support
    where support.datefrom < to_date('01.01.2006', 'dd.mm.yyyy')
    and support.t_relief_i = t_relief_ipar.fgetflextypologyclassitem_i(t_relief_ipar.fismedicalexpenses)
    and not exists
    (select null
    from requesterstage
    where requesterstage.requester_i = support.requester_i
    and support.datefrom < nvl(requesterstage.dateto, support.datefrom + 1)
    and nvl(support.dateto, requesterstage.datefrom + 1) > requesterstage.datefrom)
    ListToAddAnalyzed_1 as
    (select requester_i,
    datefrom,
    dateto,
    decode(datefrom,lag(dateto) over (partition by requester_i order by datefrom),0,1) data_set_start
    from ListToAdd),
    ListToAddAnalyzed_2 as
    (select requester_i,
    datefrom,
    dateto,
    data_set_start,
    sum(data_set_start) over(order by requester_i, datefrom ) data_set_id
    from ListToAddAnalyzed_1)
    select requesterstage_iseq.nextval,
    requester_i,
    t_requesterstage_ipar.fgetflextypologyclassitem_i(t_requesterstage_ipar.fisbefore2006),
    datefrom,
    decode(sign(nvl(dateto, to_date('01.01.2006', 'dd.mm.yyyy')) -to_date('01.01.2006', 'dd.mm.yyyy')), 0, to_date('01.01.2006', 'dd.mm.yyyy'), -1, dateto, 1, to_date('01.01.2006', 'dd.mm.yyyy'))
    from ( select requester_i
    , min(datefrom) datefrom
    , max(dateto) dateto
    From ListToAddAnalyzed_2
    group by requester_i, data_set_id
    );

  • SQL Query With analytical function

    Hi
    Below is the scenario which i am looking for in sql query using analytical functions
    I/p
    Col1
    50
    0
    -150
    -200
    300
    -100
    -300
    500
    -100
    O/p
    Col1          col2
    50                 0
    0                   0
    -150          -100
    -200              -200
    300               0
    -100              0
    -300              -100
    500               400
    -100              0Any help really appreciated
    Thanks in advance
    Edited by: unique on Aug 10, 2010 4:53 AM
    Edited by: unique on Aug 10, 2010 4:55 AM
    Edited by: unique on Aug 10, 2010 4:55 AM

    Oh,In this case,There is OLAP solution ;-)
    OLAP samples of my homepage http://www.geocities.jp/oraclesqlpuzzle/oracle-sql1-olap.html
    with work(SK,Val) as(
    select  1,  50 from dual union
    select  2,   0 from dual union
    select  3,-150 from dual union
    select  4,-200 from dual union
    select  5, 300 from dual union
    select  6,-100 from dual union
    select  7,-300 from dual union
    select  8, 500 from dual union
    select  9,-100 from dual)
    select SK,Val,GID,
    case when Val > 0
         then greatest(0,sum(Val) over(partition by GID))
         else Least(0,Val+greatest(0,sum(Val) over(partition by GID
                                     order by SK rows between unbounded preceding
                                                          and 1 preceding)))
         end as COL3
    from (select SK,Val,
          sum(greatest(0,sign(Val))) over(order by SK) as GID
          from work)
    order by SK;
    SK   VAL  GID  COL3
    1    50    1     0
    2     0    1     0
    3  -150    1  -100
    4  -200    1  -200
    5   300    2     0
    6  -100    2     0
    7  -300    2  -100
    8   500    3   400
    9  -100    3     0

  • SQL Analytical Functions in 9i

    Hi
    I am trying to determine of the SQL Analytical Functions in Oracle 9i like LAG, NTILE, PERCENT_RANK etc are part of Enterprise without OLAP installed.
    They will be very useful to a set of queries I am trying to build but I don't want to add OLAP to the mix if I can help it as the customer won't be OLAP enabled
    Cheers

    Hi
    I am trying to determine of the SQL Analytical Functions in Oracle 9i like LAG, NTILE, PERCENT_RANK etc are part of Enterprise without OLAP installed.
    They will be very useful to a set of queries I am trying to build but I don't want to add OLAP to the mix if I can help it as the customer won't be OLAP enabled
    Cheers

  • Surprised that 1Z0-047 (SQL Expert) does not include Analytic Functions

    Apparently, from a reading of the 1Z0-047 outline,  Analytic Functions are not "in scope" for the Exam.
    Comments, anyone ?
    Hemant K Chitale

    Comments, anyone ?
    There are definitely some gaps in the SQL coverage on that exam. I could see the exam being redone (and improved) for 12C much as 1Z0-144 was rewritten for 11G as a much improved version of 1Z0-147.  I'd like to see them remove the individual listing of topics pulled from SQL Fundamentals.  They should be replaced with either a much-compressed list or a single note that knowledge of SQL fundamentals is assumed.  This might help with the level of confusion newbies have over which test they should take.
    Adding all of the functions you mention would not only make the test more comprehensive but also allow the tone of the exam to be altered.  Right now I consider it to be filled with questions intended to misdirect the test candidate.  The questions are 'tricky' rather than 'difficult'. This is largely because SQL is inherently easy to read so the only way to make it hard *is* to write deliberately bad SQL. Adding more advanced topics would allow for more questions that don't have to be made artificially more difficult.

  • SQL using analytical function

    Hi all,
    I want an help in creating my SQL query to extract the data described below:
    I have one table example test containing data like below:
    ID     Desc     Status
    1     T1          DEACTIVE
    2     T2          ACTIVE
    3     T3          SUCCESS
    4     T4          DEACTIVE
    The thing i want to do is selecting all lines with ACTIVE status in this table but is there is no ACTIVE status, my query will give me the last line with DEACTIVE status.
    Can I do this in one query by using analytical function for example, if yes can yiu help me on thaht query.
    regards,
    Raluce

    Hi, Raluce,
    Here's one way to do that:
    WITH got_r_num AS
        SELECT  deptno, ename, job, hiredate
        ,       ROW_NUMBER () OVER ( PARTITION BY  deptno
                                     ORDER BY      job
                                     ,             hiredate  DESC
                                   )  AS r_num
        FROM    scott.emp
        WHERE   job  IN ('ANALYST', 'CLERK')
    SELECT     deptno, ename, job, hiredate
    FROM       got_r_num
    WHERE      job     = 'ANALYST'
    OR         r_num   = 1
    ORDER BY   deptno
    Since I don't have a sample version of your table, I used scott.emp to illustrate.
    Output:
        DEPTNO ENAME      JOB       HIREDATE
            10 MILLER     CLERK     23-JAN-82
            20 SCOTT      ANALYST   19-APR-87
            20 FORD       ANALYST   03-DEC-81
            30 JAMES      CLERK     03-DEC-81
    This query finds all ANALYSTs in each department, regardless of how many there are.  (Deptno 20 happens to have 2 ANALYSTs.)  If there is no ANALYST in a department, then the most recently hired CLERK is included.  (Deptnos 10 and 30 don't have any ANALYSTs.)
    This "partitions", or sub-divides, the table into separate units, one for each department.  In the problem you posted, it looks like you want to operate in the entire table, without sub-dividing it in any way.  To do that, just omit the PARTITION BY clause in the analytic ROW_NUMBER function, like this:
    WITH got_r_num AS
        SELECT  deptno, ename, job, hiredate
        ,       ROW_NUMBER () OVER ( --  PARTITION BY  deptno
                                     ORDER BY      job
                                     ,             hiredate  DESC
                                   )  AS r_num
        FROM    scott.emp
        WHERE   job  IN ('ANALYST', 'CLERK')
    SELECT     deptno, ename, job, hiredate
    FROM       got_r_num
    WHERE      job     = 'ANALYST'
    OR         r_num   = 1
    ORDER BY   deptno

  • About FIRST_ROW analytic function; can anyone help?

    Hi everyone,
    Can anyone help me with this simple query?
    Let's suppose I have this query (the with clause contains some data):
    WITH T AS (
    SELECT 'TEST' as COL1, 1 as COL2, 'z' as COL3 FROM dual
    UNION ALL
    SELECT 'TEST', 2, 'y' FROM dual
    UNION ALL
    SELECT 'TEST', 2, 'h' FROM dual
    SELECT FIRST_VALUE(COL1) OVER (PARTITION BY COL1), COL2, COL3
      FROM T;I would like to have only the first row returned. I was thinking that with FIRST_VALUE it will be possible, but it returns 3 records.
    So can anyone help me to have only the first record returned?
    TEST     1     zThis is just a simple example. In reality I have thousands of records. I need to get only the first record based on the name (TEST in this example). We don't really care about the other columns.
    Thanks for your help,

    user13117585 wrote:
    I would like to have only the first row returned. I was thinking that with FIRST_VALUE it will be possible, but it returns 3 records. Analytic functions don't filter rows, they just calculate values from some part of the result set.
    Aggregating is the most efficient way of doing this query:
    SQL> WITH T AS (
      2  SELECT 'TEST' as COL1, 1 as COL2, 'z' as COL3 FROM dual
      3  UNION ALL
      4  SELECT 'TEST', 2, 'y' FROM dual
      5  UNION ALL
      6  SELECT 'TEST', 2, 'h' FROM dual
      7  )
      8  select col1
      9       , min(col2) col2
    10       , max(col3) keep (dense_rank first order by col2) col3
    11    from t
    12   group by col1
    13  /
    COL1       COL2 C
    TEST          1 z
    1 row selected.Regards,
    Rob.

  • Analytic Functions in PL/SQL

    This procedure won't compile - the word PARTITION seems to be the problem - with this error...
    PLS-00103: Encountered the symbol "(" when expecting one of the following: , from
    The query in the cursor runs correctly as a stand-alone query. Can analytic functions not be used in PL/SQL cursors?
    Thanks.
    CREATE OR REPLACE
    PROCEDURE TestAnalyticFunction IS
    CURSOR GetAllTransTypes_Cursor IS
    select transaction_class.trans_desc,
    transaction_code.trans_type ,
    transaction_code.trans_code,
    transaction_code.trans_code_desc,
    sum(tr_tx_amt) as trans_sum,
    RATIO_TO_REPORT(sum(tr_tx_amt)) OVER
    (PARTITION BY transaction_code.trans_type) AS Percentage
    from transaction_code,
    transaction_class,
    transactions
    where TR_POST_DT IS NOT NULL
    AND TR_POST_DT >= '01-DEC-2000'
    AND TR_POST_DT <= '31-JAN-2001'
    AND TRANSACTION_CODE.TRANS_CLASS = TRANSACTION_CLASS.TRANS_CLASS_ID
    AND TRANSACTION_CODE.TRANS_CODE = TRANSACTIONS.TR_TX_CODE
    AND TRANSACTION_CODE.TRANS_TYPE in (1,2,3,4,5,8)
    group by transaction_code.trans_type,
    trans_class,
    trans_desc,
    trans_code,
    trans_code_desc
    order by transaction_code.trans_type, trans_code;
    TYPE TransClassDescType IS TABLE OF transaction_class.trans_desc%TYPE;
    TYPE TransCodeTypeType IS TABLE OF transaction_code.trans_type%TYPE;
    TYPE TransCodeCodeType IS TABLE OF transaction_code.trans_code%TYPE;
    TYPE TransCodeDescType IS TABLE OF transaction_code.trans_code_desc%TYPE;
    TYPE TotalType IS TABLE OF NUMBER(14,2);
    TYPE TotalPctType IS TABLE OF NUMBER(6, 2);
    TransClassDesc TransClassDescType;
    TransCodeType TransCodeTypeType;
    TransCodeCode TransCodeCodeType;
    TransCodeDesc TransCodeDescType;
    Total TotalType;
    TotalPct TotalPctType;
    BEGIN
    OPEN GetAllTransTypes_Cursor;
    FETCH GetAllTransTypes_Cursor BULK COLLECT INTO TransClassDesc,TransCodeType,TransCodeCode,TransCodeDesc,
    Total, TotalPct;
    CLOSE GetAllTransTypes_Cursor;
    END TestAnalyticFunction;
    null

    Some functions just don't seem to work in PL/SQL even though they work fine in SQL*Plus.
    Two such functions I found were NVL2 and RATIO_TO_REPORT.
    Have no clue why yet.
    <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by Dale Johnson ([email protected]):
    This procedure won't compile - the word PARTITION seems to be the problem - with this error...
    PLS-00103: Encountered the symbol "(" when expecting one of the following: , from
    The query in the cursor runs correctly as a stand-alone query. Can analytic functions not be used in PL/SQL cursors?
    Thanks.
    CREATE OR REPLACE
    PROCEDURE TestAnalyticFunction IS
    CURSOR GetAllTransTypes_Cursor IS
    select transaction_class.trans_desc,
    transaction_code.trans_type ,
    transaction_code.trans_code,
    transaction_code.trans_code_desc,
    sum(tr_tx_amt) as trans_sum,
    RATIO_TO_REPORT(sum(tr_tx_amt)) OVER
    (PARTITION BY transaction_code.trans_type) AS Percentage
    from transaction_code,
    transaction_class,
    transactions
    where TR_POST_DT IS NOT NULL
    AND TR_POST_DT >= '01-DEC-2000'
    AND TR_POST_DT <= '31-JAN-2001'
    AND TRANSACTION_CODE.TRANS_CLASS = TRANSACTION_CLASS.TRANS_CLASS_ID
    AND TRANSACTION_CODE.TRANS_CODE = TRANSACTIONS.TR_TX_CODE
    AND TRANSACTION_CODE.TRANS_TYPE in (1,2,3,4,5,8)
    group by transaction_code.trans_type,
    trans_class,
    trans_desc,
    trans_code,
    trans_code_desc
    order by transaction_code.trans_type, trans_code;
    TYPE TransClassDescType IS TABLE OF transaction_class.trans_desc%TYPE;
    TYPE TransCodeTypeType IS TABLE OF transaction_code.trans_type%TYPE;
    TYPE TransCodeCodeType IS TABLE OF transaction_code.trans_code%TYPE;
    TYPE TransCodeDescType IS TABLE OF transaction_code.trans_code_desc%TYPE;
    TYPE TotalType IS TABLE OF NUMBER(14,2);
    TYPE TotalPctType IS TABLE OF NUMBER(6, 2);
    TransClassDesc TransClassDescType;
    TransCodeType TransCodeTypeType;
    TransCodeCode TransCodeCodeType;
    TransCodeDesc TransCodeDescType;
    Total TotalType;
    TotalPct TotalPctType;
    BEGIN
    OPEN GetAllTransTypes_Cursor;
    FETCH GetAllTransTypes_Cursor BULK COLLECT INTO TransClassDesc,TransCodeType,TransCodeCode,TransCodeDesc,
    Total, TotalPct;
    CLOSE GetAllTransTypes_Cursor;
    END TestAnalyticFunction;<HR></BLOCKQUOTE>
    null

  • Help with analytical function

    I successfully use the following analytical function to sum all net_movement of a position (key for a position: bp_id, prtfl_num, instrmnt_id, cost_prc_crncy) from first occurrence until current row:
    SELECT SUM (net_movement) OVER (PARTITION BY bp_id, prtfl_num, instrmnt_id, cost_prc_crncy ORDER BY TRUNC (val_dt) RANGE BETWEEN UNBOUNDED PRECEDING AND 0 FOLLOWING) holding,
    what i need is another column to sum net_movement of a position but only for the current date, but all my approaches fail..
    - add the date (val_dt) to the 'partition by' clause and therefore sum only values with same position and date
    SELECT SUM (net_movement) OVER (PARTITION BY val_dt, bp_id, prtfl_num, instrmnt_id, cost_prc_crncy ORDER BY TRUNC (val_dt) RANGE BETWEEN UNBOUNDED PRECEDING AND 0 FOLLOWING) today_net_movement
    - take the holding for the last date and subtract it from the current holding afterwards
    SELECT SUM (net_movement) OVER (PARTITION BY bp_id, prtfl_num, instrmnt_id, cost_prc_crncy ORDER BY TRUNC (val_dt) RANGE BETWEEN UNBOUNDED PRECEDING AND -1 FOLLOWING) last_holding,
    - using lag on the analytical function which calculates holding fails too
    I also want to avoid creating a table which stores the last holding..
    Does anyone sees where I make a mistake or knows an alternative to get this value?
    It would help me much!
    Thanks in advance!

    Thank you,
    but I already tried that but it returns strange values which are not the correct ones for sure.
    It is always the same value for each row, if its not 0, and a very high one (500500 for example), even if the sum of all net_movement of that date is 0 (and the statement for holding returns 0 too)
    I also tried witch trunc(val_dt,'DDD') with the same result (without trunc it is the same issue)
    please help if you can, thanks in advance!

Maybe you are looking for