Top-N Analysis query in explicit cursor

Hi all,
I am using developer tool Reports 6i and database 10g
i have written a cursor query
cursor cur_voy is
select * from(select bvd_voy,bvd_vessel from bl_details,bl_voyage_details
where bdl_company =bvd_company
order by bvd_reference,bvd_serial asc)
where rownum<=1;
but its show error :- Encountered the symbol 'order' when expecting one of the following
.(),*,@,%,&,-,+,/,mod,rem ..........
why it shows errors ?
please help
Thanks in advance
Rinz

Hi,
Please post the complete block/proc...
The sql seems ok..
similar logic posted below...
declare
cursor c1 is
WITH t AS
     (SELECT 1 ID, 'AA' NAME
        FROM DUAL
      UNION ALL
      SELECT 2 ID, 'BB' NAME
        FROM DUAL
      UNION ALL
      SELECT 3 ID, 'CC' NAME
        FROM DUAL),
     t2 AS
     (SELECT 1 ID, 'DD' name2
        FROM DUAL
      UNION ALL
      SELECT 2 ID, 'EE' name2
        FROM DUAL
      UNION ALL
      SELECT 3 ID, 'FF' name2
        FROM DUAL)
SELECT *
  FROM (SELECT   NAME, name2
            FROM t, t2
           WHERE t.ID = t2.ID
        ORDER BY t.ID, t2.ID asc)
where rownum<=1      ;  
begin
null;
end;

Similar Messages

  • Performing Top-N Analysis

    An Oracle University Material in Sql Says
    The high-level structure of a Top-N analysis query is:
    SELECT [column_list], ROWNUM
    FROM (SELECT [column_list]
    FROM table
    ORDER BY Top-N_column)
    WHERE ROWNUM <= N;
    For example to display the top three earner names and salaries from the EMPLOYEES table:
    SELECT ROWNUM as RANK, last_name, salary
    FROM (SELECT last_name,salary FROM employees
    ORDER BY salary DESC)
    WHERE ROWNUM <= 3;
    My question is
    If, instead of this query, I write
    1)
    SELECT ROWNUM as RANK, last_name, salary
    FROM employees
    WHERE ROWNUM <= 3
    ORDER BY salary DESC
    or
    2)
    SELECT ROWNUM as RANK, last_name, salary
    FROM ( SELECT last_name,salary
    FROM employees
    WHERE ROWNUM <= 3
    ORDER BY salary DESC
    is any difference?
    The results in schema hr are the same.............
    Thank you

    is any difference? yes, there is!
    SQL> with t as (select 1 num from dual union all
      2             select 4 from dual union all
      3             select 3 from dual union all
      4             select 2 from dual)
      5             --
      6  SELECT ROWNUM as RANK, num
      7    FROM (SELECT  num FROM t ORDER BY num desc)
      8   WHERE ROWNUM <= 3;
          RANK        NUM
             1          4
             2          3
             3          2
    SQL>
    SQL> with t as (select 1 num from dual union all
      2             select 4 from dual union all
      3             select 3 from dual union all
      4             select 2 from dual)
      5  --
      6  SELECT ROWNUM as RANK, num
      7    FROM t
      8   WHERE ROWNUM <= 3
      9   ORDER BY num DESC
    10  /
          RANK        NUM
             2          4
             3          3
             1          1
    SQL>
    SQL> with t as (select 1 num from dual union all
      2             select 4 from dual union all
      3             select 3 from dual union all
      4             select 2 from dual)
      5  --
      6  SELECT ROWNUM as RANK, num
      7    FROM (SELECT num FROM t
      8          WHERE ROWNUM <= 3
      9          ORDER BY num DESC)
    10  /
          RANK        NUM
             1          4
             2          3
             3          1
    SQL> rownum is confered before ordering is made.
    Thats why you should place the subquery with ordering in the inline view.

  • Difference between implicit cursor and explicit cursor.

    hi all,
    i have used explicit cursor, i know the way of using implicit cursor, but i don't what the difference between both.
    Is they both are different. if yes y? which cursor is most reliable to use.
    please tell me..
    Thanks..

    OK,
    1- An IMPLICIT cursor is automatically declared by Oracle every time an SQL statement is executed The user will not be aware of this happening and will not be able to control or process
    the information in an implicit cursor.
    Example:
    SET SERVEROUTPUT ON
    BEGIN
    UPDATE Customers
    SET Cust_name = 'B'
    WHERE Cust_name LIKE 'B%';
    DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT);
    END;
    2- An EXPLICIT cursor is defined by the program for any query that returns more than one row of data.
    That means the programmer has declared the cursor within the PL/SQL code block. This declaration allows for the application to sequentially process each row of data as it is returned by the cursor.
    Example :
    DECLARE
    CURSOR C_MyCursor IS
    SELECT *
    FROM bookings
    WHERE Cust_no = 701;
    see this post
    http://comsci.liu.edu/~vasilaky/db2/cursors.htm :-)

  • Top n Analysis using correlated subquery

    Please explain this query. It is doing top n analysis using correlated subquery. I need explaination of execution of this query.
    Select distinct a.sal
    From emp a
    where 1=(select count ( distinct b.sal) from emp b
         where a.sal <=b.sal)
    Thanks in advance

    Try breaking the query down and rewriting it in order to follow the logic;
    SQL> --
    SQL> -- Start by getting each salary from emp along with a count of all salaries in emp
    SQL> --
    SQL> select   a.sal,
            (select count (distinct b.sal) from scott.emp b ) count_sal
    from scott.emp a
    order by 1 desc
           SAL  COUNT_SAL
          5000         12
          3000         12
          3000         12
          2975         12
          2850         12
          2450         12
          1600         12
          1500         12
          1300         12
          1250         12
          1250         12
          1100         12
           950         12
           800         12
    14 rows selected.
    SQL> --
    SQL> --Add a condition to the count for only salaries below or equal to the current salarySQL> --
    SQL> select   a.sal,
            (select count (distinct b.sal) from scott.emp b where a.sal <=b.sal) rank_sal
    from scott.emp a
    order by 1 desc
           SAL   RANK_SAL
          5000          1
          3000          2
          3000          2
          2975          3
          2850          4
          2450          5
          1600          6
          1500          7
          1300          8
          1250          9
          1250          9
          1100         10
           950         11
           800         12
    14 rows selected.
    SQL> --
    SQL> -- Add a condition to only pick the nth highest salary
    SQL> --
    SQL> select    a.sal,
             (select count (distinct b.sal) from scott.emp b where a.sal <=b.sal) rank_sal
    from scott.emp a
    where (select count (distinct b.sal) from scott.emp b where a.sal <=b.sal) = 4
           SAL   RANK_SAL
          2850          4
    1 row selected.Hope this helps.

  • Top n analysis using hierarchial queries

    hi all,
    can we do top n analysis in hierarchial queries using level pseudo columns. if so please give an example.
    thanks and regards,
    sri ram.

    Hi,
    Analytic functions (such as RANK) often interfere with CONNECT BY queries. Do one of them in a sub-query, and the other in a super-query, as shown below.
    If you do the CONNECT BY first, use ROWNUM (which is assigned after ORDER SIBLINGS BY is applied) to preserve the order of the CONNECT BY query.
    WITH     connect_by_results     AS
         SELECT     LPAD ( ' '
                   , 3 * (LEVEL - 1)
                   ) || ename          AS iname
         ,     sal
         ,     ROWNUM               AS r_num
         FROM     scott.emp
         START WITH     mgr     IS NULL
         CONNECT BY     mgr     = PRIOR empno
         ORDER SIBLINGS BY     ename
    SELECT       iname
    ,       sal
    ,       RANK () OVER (ORDER BY sal DESC)     AS sal_rank
    FROM       connect_by_results
    ORDER BY  r_num
    ;Output:
    INAME                  SAL   SAL_RANK
    KING                  5000          1
       BLAKE              2850          5
          ALLEN           1600          7
          JAMES            950         13
          MARTIN          1250         10
          TURNER          1500          8
          WARD            1250         10
       CLARK              2450          6
          MILLER          1300          9
       JONES              2975          4
          FORD            3000          2
             SMITH         800         14
          SCOTT           3000          2
             ADAMS        1100         12 
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and the results you want from that data. If you use only commonly available tables (such as those in the scott or hr schemas), then you don't have to post any sample data; just post the results.
    Explain how you get those results from that data.
    Always say what version of oracle you're using.

  • Top-n analysis in oracle 8i

    How to use top-n analysis in oracle 8i?
    I mean,take a example.
    I am maintaining a database of a 1000 employees.I want to display the names of the employees who are getting top 10 salaries(more further top 100 salaries) using a SQL query in oracle 8i only.Please answer my problem.

    Sorry, my suggestion will return 10 emp with highest salaries, not all employees with 10 highest salaries. To get all employees with 10 highest salaries in 8i:
    SQL> SELECT  ename,
      2          sal
      3    FROM  emp
      4    WHERE sal IN (
      5                  SELECT  sal
      6                    FROM  (
      7                           SELECT  sal
      8                             FROM  emp
      9                             GROUP BY sal
    10                             ORDER BY sal DESC
    11                          )
    12                    WHERE rownum <= 10
    13                 )
    14  /
    ENAME             SAL
    KING             5000
    FORD             3000
    SCOTT            3000
    JONES            2975
    BLAKE            2850
    CLARK            2450
    ALLEN            1600
    TURNER           1500
    MILLER           1300
    MARTIN           1250
    WARD             1250
    ENAME             SAL
    ADAMS            1100
    12 rows selected.
    SQL> SY.

  • What is Top-N analysis

    What is Top-N analysis.?? can some one explain. I encountered this while going thru
    the Sql book
    The ORDER BY clause in the subquery is not
    needed unless you are performing Top-N analysis.

    That means you are doing something like this in order to get top five paid people::
    SELECT * FROM
    (  SELECT empno, ename, sal+nvl(comm,0) as remuneration
       FROM   emp
       ORDER BY remuneration DESC )
    WHERE rownum <= 5
    /We have to do the ORDER BY in a sub-query because rownum gets applied before the rows are sorted.
    Cheers, APC

  • TOP N analysis with same values

    Dear Members,
    Suppose we have the following data in the table Student.
    Sname GPA
    Jack 4.0
    Smith 3.7
    Rose 3.5
    Rachel 3.5
    Ram 2.8
    I have seen many questions in this forum which gives good queries for TOP N analysis. But in my case those are not working.
    There are total 5 students. I should write a query which should take an input and should give the students with top gpa as output in desc order.
    Suppose if i give 4 as input i must get 4,3.7,3.5,3.5,2.8Gpa's since we have 2 gpa's which are same. Suppose i give 3 as the input i must get 4,3.7,3.5 and 3.5 GPA's.
    The query must consider the GPA's which are same as one not different. How can we achive this. i.e the top three students (suppose input is 3) must be
    Jack 4.0
    Smith 3.7
    Rose 3.5
    Rachel 3.5
    It must also include Rachel.
    Any help is greatly appreciated.
    Thanks
    Sandeep

    SQL> select * from test;
    NAME                        GPA
    Jack                          4
    Smith                       3.7
    Rose                        3.5
    Rachel                      3.5
    Ram                         2.8
    SQL> select name,gpa
      2  from
      3    (select name,gpa,dense_rank() over(order by gpa desc) rn
      4     from test)
    5 where rn <= 3
      6  order by rn;
    NAME                        GPA
    Jack                          4
    Smith                       3.7
    Rose                        3.5
    Rachel                      3.5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • WebIntelligence report on top of bex Query in BI4.0 (SP2)

    Dear experts,
    I'm working on webi report on top of bex Query in BI4.0 (SP2). I would like to know your opinions about some points I have noticed.
    A - I need to drill throught a hierarchy which contains several millions of members.
    Is there a possibility in my report drilling down to one member without selecting all members of the hierarchy in the query panel ?
    B - I cannot put a dimension with hierarchy both in the result pane and in the filter pane, although it is possible for any other dimension without hierarchy.
    Is there any special trick to achieve this or is a bug??
    C - I cannot sort variables defined in my bex query, but they don't appear in the window to sort prompts.
    Do you know how to solve this issue ?
    D - I noticed all keys and description of dimensions appear with the same label "key" and "text". SO it is very confusing for the user to understand what key or text means (if a key if the customer key or the company code key).
    Any idea to have a more explicit label for BW characteristics?
    Regards.
    CL.

    Hi,
    I do not have access to a BW system to test this, but i have something on three of your questions...
    A) drilling without getting all the lower members. I would look to query drill, this means a drill action in the report will go to the database to fetch the level (and only for the selected member, a drill down).
    The only thing I'm not sure about is if the BEx hierarchy is seen as drill hierarchy by that option too.
    If you'd ask me it really should.
    C) this is an old problem, but I know it is bugging more people, because the same is true for universe based prompts. They also cannot be 'arranged'. Since the Bex query is the 'universe', ordering that has been set in the 'universe' is the only chance you have.
    Like with the universe you could use prefixes to separate them from the query defined prompts (based on an alphabetic sort of the prompt message). I'd say this is a good one for the idea place...
    D) also an old (very old actually) problem. Some will want to keep the text short and simple. The prefixing will tell which key it is. Some will prefer unique names, so no prefix is needed. The same was true for universe designers. Now since there is no universe, if you do not agree with the 'designer' you're out of luck  no way to change it.
    Hope this helps,
    Marianne

  • Are Explicit Cursors better than Select Statement

    Hi Gurus,
    I came across this opinion that explicit cursors are better (even if the query returns a single row) instead of a single row SELECT statement within the code in terms of performance.
    Is that true??? Pls elaborate in either case.
    Can i hear it from Sri/Andrew ???
    Thanks for ur time...
    Peyush

    no, it is just the opposite
    see
    http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:1205168148688
    for a complete explanation why
    greetings
    Freek
    Hi Gurus,
    I came across this opinion that explicit cursors are better (even if the query returns a single row) instead of a single row SELECT statement within the code in terms of performance.
    Is that true??? Pls elaborate in either case.
    Can i hear it from Sri/Andrew ???
    Thanks for ur time...
    Peyush

  • Difference between Implicit and Explicit Cursors

    Hi All,
    Can you just tell me what is the difference between implicit and explicit cursor?
    Thanks,
    Padma

    Hi ,
    Implicit Cursor means ,the cursors which are defined impplicitly.Here we use curosr variables as "SQL%found,sql%not found,sql%is open.."
    but explicit cursors defined by us like if u create a cursor C1 then
    c1%found,c1% not found ,c1%open
    Thank you.

  • Implicit and explicit cursors

    i want to know when to it is recommended to use the implicit cursors (for rec in….) and when it is recommended to use explicit cursors(open cursors….)

    As Brett said, both methods require an explicit cursor definition. What is implicit in the FOR cursor loop is the opening and closing of the cursor. If you use a cursor FETCH loop, you need to code that explicitly yourself.
    My recommendation is to use the cursor FETCH loop and code those two additional OPEN CURSOR and CLOSE CURSOR statements. Reason: a cursor fetch loop allows for proper bulk processing code to be used. It does not rely on the implicit bulk fetching (of 10 rows per time) of a FOR cursor loop - which btw is only available in 10G.
    A FOR cursor loop does not scale. You cannot make the bulk fetching larger. Or smaller. You cannot do bulk updates or inserts in the loop.
    So I do not care what the experts think and say, for me the answer is clear cut. You want to write scalable and performant code? You will use a cursor fetch loop. Not a for cursor loop.

  • What is the real time use of implicit and explicit cursors in pl/sql

    what is the real time use of implicit and explicit cursors in pl/sql.............please tell me

    You can check the following link ->
    http://www.smart-soft.co.uk/Oracle/oracle-plsql-tutorial-part5.htm
    But, i've a question ->
    Are you student?
    Regards.
    Satyaki De.

  • Ref Cursor over Implicit and explicit cursors

    Hi,
    In my company when writing PL/SQL procedure, everyone uses "Ref Cursor",
    But the article below, says Implicit is best , then Explicit and finally Ref Cursor..
    [http://www.oracle-base.com/forums/viewtopic.php?f=2&t=10720]
    I am bit confused by this, can any one help me to understand this?
    Thanks

    SeshuGiri wrote:
    In my company when writing PL/SQL procedure, everyone uses "Ref Cursor",
    But the article below, says Implicit is best , then Explicit and finally Ref Cursor..
    [http://www.oracle-base.com/forums/viewtopic.php?f=2&t=10720]
    I am bit confused by this, can any one help me to understand this?There is performance and there is performance...
    To explain. There is only a single type of cursor in Oracle - that is the cursor that is parsed and compiled by the SQL engine and stored in the database's shared pool. The "+client+" is then given a handle (called a SQL Statement Handle in many APIs) that it can use to reference that cursor in the SQL engine.
    The performance of this cursor is not determined by the client. It is determined by the execution plan and how much executing that cursor cost ito server resources.
    The client can be Java, Visual Basic, .Net - or a PL/SQL program. This client language (a client of SQL), has its own structures in dealing with that cursor handle received from the SQL engine.
    It can hide it from the developer all together - so that he/she does not even see that there is a statement handle. This is what implicit cursors are in PL/SQL.
    It can allow the developer to manually define the cursor structure - this is what explicit cursors, ref cursors, and DBMS_SQL cursors are in PL/SQL.
    Each of these client cursor structures provides the programmer with a different set of features to deal with SQL cursor. Explicit cursor constructs in PL/SQL do not allow for the use of dynamic SQL. Ref cursors and DBMS_SQL cursors do. Ref cursors do not allow the programmer to determine, at run-time, the structure of the SQL projection of the cursor. DBMS_SQL cursors do.
    Only ref cursors can be created in PL/SQL and then be handed over to another client (e.g. Java/VB) for processing. Etc.
    So each of the client structures/interfaces provides you with a different feature set for SQL cursors.
    Choosing implicit cursors for example does not make the SQL cursor go faster. The SQL engine does not know and does not care, what client construct you are using to deal with the SQL cursor handle it gave you. It does not matter. It does not impact its SQL cursor performance.
    But on the client side, it can matter - as your code in dealing with that SQL cursor determines how fast your interaction with that SQL cursor is. How many context switches you make. How effectively you use and re-use the SQL (e.g. hard parsing vs soft parsing vs re-using the same cursor handle). Etc.
    Is there any single client cursor construct that is better? No.
    That is an ignorant view. The client language provides a toolbox, where each tool has a specific application. The knowledgeable developer will use the right tool for the job. The idiot developer will select one tool and use it as The Hammer to "solve" all the problems.

  • Sales Analysis Query

    Dear Experts,
    Want to Sales Analysis Query same as Sales Analysis in SAP
    In standard SAP when we run report with anual slection criteria for eg, 01/04/2010 To 31/03/12
    it's show in one column not display in compare with Quaterwise & Monthwise report.
    Result for query is
    Customer Code, Customer Name, Year2010 (Sales), Year2011 (Sales), Year2012 (Sales)
    Please help me.
    BR
    Deep

    Ok........
    It was my query mistake.......
    Please try this......
    SELECT Distinct T0.CardCode,T0.CardName, 
    (isnull((SELECT SUM((case when SUBSTRING((CONVERT(VARCHAR(11),T4.DOCDATE,106)),8,11) like '2006%' then T4.LineTotal else 0 end))
          FROM INV1 T4 WHERE T4.ItemCode=T1.ItemCode ),0)) '2006',
    (isnull((SELECT SUM((case when SUBSTRING((CONVERT(VARCHAR(11),T4.DOCDATE,106)),8,11) like '2007%' then T4.LineTotal else 0 end))
          FROM INV1 T4 WHERE T4.ItemCode=T1.ItemCode ),0)) '2007',
    (isnull((SELECT SUM((case when SUBSTRING((CONVERT(VARCHAR(11),T4.DOCDATE,106)),8,11) like '2008%' then T4.LineTotal else 0 end))
          FROM INV1 T4 WHERE T4.ItemCode=T1.ItemCode ),0)) '2008',
    (isnull((SELECT SUM((case when SUBSTRING((CONVERT(VARCHAR(11),T4.DOCDATE,106)),8,11) like '2009%' then T4.LineTotal else 0 end))
          FROM INV1 T4 WHERE T4.ItemCode=T1.ItemCode ),0)) '2009',
    (isnull((SELECT SUM((case when SUBSTRING((CONVERT(VARCHAR(11),T4.DOCDATE,106)),8,11) like '2010%' then T4.LineTotal else 0 end))
          FROM INV1 T4 WHERE T4.ItemCode=T1.ItemCode ),0)) '2010',
    (isnull((SELECT SUM((case when SUBSTRING((CONVERT(VARCHAR(11),T4.DOCDATE,106)),8,11) like '2011%' then T4.LineTotal else 0 end))
          FROM INV1 T4 WHERE T4.ItemCode=T1.ItemCode ),0)) '2011',
    (isnull((SELECT SUM((case when SUBSTRING((CONVERT(VARCHAR(11),T4.DOCDATE,106)),8,11) like '2012%' then T4.LineTotal else 0 end))
          FROM INV1 T4 WHERE T4.ItemCode=T1.ItemCode ),0)) '2012'
    FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
    Group By SUBSTRING((CONVERT(VARCHAR(11),T0.DOCDATE,106)),8,11), T1.ItemCode, T0.CardName, t0.CardCode
    Regards,
    Rahul

Maybe you are looking for

  • How can I export the report to Excel or CSV format in Rational(Java)?

    <p>Dear all,</p><p>Now I develop CR report integrate with Web application, I use Ratioanl(RAD) to develop. And I want to export the report to Excel/CSV format, but always failed.</p><p>If I force it export CSV file in the system, when I use MS office

  • How do you fix the itunes R6034 error

    How do you fix the R6034 error with itunes?

  • Oracle SQL Developper and SQL Server Connection

    Hello I try to connect to a SQL Server 2005 Database under Oracle SQl Developper but I always obtain this error : oracle.jdeveloper.cm.CMException: Unable to find driver: net.sourceforge.jtds.jdbc.Driver      at oracle.dbtools.raptor.conntypes.Raptor

  • FOX Formula definition

    Greetings All!!! I am recieving an error Types of parameter RATE (P) and variable RATE1(F) are inconsistent .. I am using following FOX code DATA FISC_VAR TYPE 0FISCVARNT. DATA FISCYEAR TYPE 0FISCYEAR. DATA RATE1 TYPE F. FOREACH FISCYEAR. CALL FUNCTI

  • NI SPY records additional calls on XP machine versus NT 4.0 machine (same exe)

    Running the exact same executable on an Windows XP controller (VXIpc-870b) takes considerably longer (38.24s) than the same executable on Windows NT 4.0 controller (VXIpc-870, ). When running NI SPY to capture the calls, the first 46 are the same, bu