SELECT n. row without using cursors

For example, I retrieved 100 rows after executing a SELECT statement but I want to list only 20th row. How can I do this without using cursors?

The general approach to getting a set of rows would be to do something like
SELECT *
  FROM (SELECT a.*, rownum rn
          FROM (<<query with ORDER BY clause>>) a
         WHERE rownum <= <<max>>)
WHERE rn >= <<MIN>>Depending on the particular requirements, you could also do something like
SELECT *
  FROM (SELECT a.*, RANK() OVER (<<ORDER BY clause>>) rnk
          FROM <<table>> a
         WHERE <<rest of statement>>)
WHERE rnk = 20Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC

Similar Messages

  • Select extra row without using UNION ALL in pl/sql

    Hi,
    Can anyone tell me how to select extra row without using UNION or UNION ALL in pl/sql. Actually I want to have my o/p of query as partitioned by designation and ordered by salary and than one extra row which will contain the highest salary in a particular salary. My table has first_name,emp_id,designation and salary column. And I wnt the o/p as.
    Mohinish,12212,SI,46000
    Ram,11212,SSI,47000
    Shyam,12133,SI,48000
    Rick,9898,SI,46000
    Rocky,12312,SSI,56000
    Sariq,23948,SI,43000
    Suman,12789,HR,49000
    Sampy,12780,SI,46000
    Parna,11111,HR,50000
    Now the o/p should be.
    Mohinish,12212,SI,46000
    Rick,9898,SI,46000
    Sariq,23948,SI,43000
    Shyam,12133,SI,48000
    Shyam,12133,SI,48000
    Ram,11212,SSI,47000
    Rocky,12312,SSI,56000
    Rocky,12312,SSI,56000
    Suman,12789,HR,49000
    Parna,11111,HR,50000
    Parna,11111,HR,50000
    Thanks in Advance

    You don't have to do a UNION or UNION ALL in PL/SQL but you would need to in SQL to get the desired output:
    with data_recs
    as (select 'Mohinish' first_name,12212 emp_id,'SI' designation,46000 salary from dual union
         select 'Ram',11212,'SSI',47000 from dual union
         select 'Shyam',12133,'SI',48000 from dual union
         select 'Rick',9898,'SI',46000 from dual union
         select 'Rocky',12312,'SSI',56000 from dual union
         select 'Sariq',23948,'SI',43000 from dual union
         select 'Suman',12789,'HR',49000 from dual union
         select 'Sampy',12780,'SI',46000 from dual union
         select 'Parna',11111,'HR',50000 from dual)
    select first_name, emp_id, designation, salary from data_recs union all
    select s.first_name, s.emp_id, s.designation, s.salary
      from (select first_name,
                   emp_id,
                   designation,
                   salary,
                   row_number() over (partition by designation order by salary desc) high_salary
              from data_recs
             order by designation, salary) s
    where s.high_salary = 1
    order by designation, salary;
    FIRST_NAME  EMP_ID DESIGNATION   SALARY
    Suman        12789 HR             49000
    Parna        11111 HR             50000
    Parna        11111 HR             50000
    Sariq        23948 SI             43000
    Rick          9898 SI             46000
    Mohinish     12212 SI             46000
    Sampy        12780 SI             46000
    Shyam        12133 SI             48000
    Shyam        12133 SI             48000
    Ram          11212 SSI            47000
    Rocky        12312 SSI            56000
    Rocky        12312 SSI            56000

  • How to manipulate data in multiple rows without using cursor??

    Hi all,
    I have a form in which there is a push button & 4 text fields.
    Push button : Process
    Text fields: Year, Month, Financial_To_Year, Financial_From_Date.
    In database , there are tables like, CUSTOMER_MASTER, FD_ACCOUNT_MASTER, FD_ACCOUNT_DTL, CUSTOMER_YEARLY_INTEREST, etc.
    In table FD_ACCOUNT_MASTER, there are columns, like CUST_CODE, FD_ACCT_NO, FD_AMOUNT, ACCT_OPEN_DT, ACCT_CLOSE_DATE, ACCT_TYPE, INTEREST_RATE, etc.
    There are thousands of records in the table.
    For Push button : Process , TRIGGER: When button pressed,
    I have to do all the process of FD for all the FD_ACCOUNTS at once. Process means i have to calculate Interest for all the accounts, calculate interest monthly, quarterly, yearly and make the FD_Accounts disable after the date of ACCT_CLOSE_DATE is reached, make the accounts renewed , etc all the process.
    But to do this process for multiple rows at once, we use cursor, but i don't want to use cursor as i feel its too tedious to do.
    Is there any other way , where i do process of multiple records at once without using cursor??
    Help me, Thank You.
    Oracle Forms Builder 6i.
    Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production

    An Update statement certainly can update sets of data instead single row updates:
    UPDATE Statement
    if you can't do it in a single statement you can use bulk processing as well:
    PLSQL 101
    cheers

  • Count the no.of rows without using count function

    Hi,
    How to count the no.of rows without using the count function?
    Thanks,

    they won't be 100% accurate. You're correct, Bluefrog, but the same goes for doing a count(*) (depending on the size of the table, ofcourse):
    the table being queried might be under DML (deletes/inserts), and the next count(*) might give different results.
    Both approaches will never be 100% accurate.
    But simply selecting num_rows will be much much faster than doing a count(*).
    Counting the number of rows always reminds me of this ongoing discussion, by the way:
    http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:127412348064#14136093079164 ;)
    Usually knowing the number of records by approximatly is sufficient, imo. 1000000 or 1000007 records: I don't care, don't need to know that.
    I've never needed to know the exact number of records in a table in my code, or been given a requirement that forced me to.

  • Delete internal table rows without using loop statement

    i have an internal table which consists of 100 records.
    i need to keep only first 5 records.
    without using the loop statement i need to delete the rest of the records. how can we achieve this result.
    i.e.  delete itab1 where  "recordno"  > 5.
    regards.
    ZG

    Hi,
    Delete itab [FROM idx1] [TO idx2] {Where (log_exp)]
    To delete several lines at once, you have to specify at least one of the additions FROM, TO, or WHERE. You can only use the additions FROM and TO with standard tables and sorted tables.
    Delete itab [FROM idx1]
    If you specify FROM, all the table rows from the table index idx1 onwards are included.
    Delete itab [TO idx2]
    If you specify TO, all the table rows from the table index idx2 onwards are included.
    PARAMETERS: p_carrid TYPE sflight-carrid,
                p_connid TYPE sflight-connid.
    DATA: BEGIN OF seats,
            fldate    TYPE sflight-fldate,
            seatsocc  TYPE sflight-seatsocc,
            seatsmax  TYPE sflight-seatsmax,
            seatsfree TYPE sflight-seatsocc,
          END OF seats.
    DATA seats_tab LIKE STANDARD TABLE OF seats.
    SELECT fldate seatsocc seatsmax
           FROM sflight
           INTO TABLE seats_tab
           WHERE carrid = p_carrid AND
                 connid = p_connid.
    LOOP AT seats_tab INTO seats.
      seats-seatsfree = seats-seatsmax - seats-seatsocc.
      MODIFY seats_tab INDEX sy-tabix FROM seats.
    ENDLOOP.
    ENDLOOP.
    SORT seats_tab BY seatsfree DESCENDING.
    DELETE seats_tab FROM 5.
    Thanks & Regards,
    ShreeMohan
    Edited by: ShreeMohan Pugalia on Jul 21, 2009 4:28 PM

  • How to return a resultset in a stored procedure without using Cursor

    Dear all,
    I need to return a resultset in a stored procedure.
    I know I can return a Cursor.
    But because there are many complicate logics I need to carry out,
    so I can finish all the logic in a SINGLE SQL select statement, so I think it can't
    use Cursor as return value.
    Does anybody know other approaches? Please help.
    Thanks!

    Some basic techniques that one can use in SQL. These are merely to illustrate (simplistically) different approaches.
    > field3 = select count(*) from table2,table1 where table2.id = table1.id
    This can be done via an analytical function (refer to the SQL Reference) or very simply, as an in-line select as follows:
    SQL> select
    2 o.object_name,
    3 (select count(*) from user_objects u where u.object_type = o.object_type) as "ONE OF",
    4 o.object_type
    5 from user_objects o
    6 where o.object_name = 'LISTFILES';
    OBJECT_NAME ONE OF OBJECT_TYPE
    LISTFILES 8 PROCEDURE
    So LISTFILE is "one of 8" procedures. I would however rather so this via an analytic function as these are a lot more flexible. Note the WITH clause that allows "modularisation" of SQL - kind of like creating "sub-SQL results" in a single SQL statement:
    SQL> with DATASET1 as(
    2 select
    3 object_name,
    4 count(distinct object_name) over (partition by object_type) as TOTAL_TYPES,
    5 object_type
    6 from user_objects
    7 ),
    8 DATASET2 as(
    9 select
    10 name,
    11 text as SOURCE_LINE,
    12 line as LINE_NO,
    13 count(line) over (partition by name) as TOTAL_LINES
    14 from user_source
    15 )
    16 select
    17 object_name,
    18 '1 of '||total_types||' '||object_type as "TYPE",
    19 source_line as "1st LINE",
    20 total_lines as "TOTAL LINES"
    21 from DATASET1 ds1,
    22 DATASET2 ds2
    23 where ds1.object_name = ds2.name
    24 and ds1.object_name = 'LISTFILES'
    25* and ds2.line_no = 1
    SQL> /
    OBJECT_NAME TYPE 1st LINE TOTAL LINES
    LISTFILES 1 of 8 PROCEDURE procedure ListFiles( cDirectory in varchar2 ) 3
    SQL>
    SQL is quite powerful and analytical functions allows all kinds of aggregation processing per row, accessing the leading or lagging rows' data, etc.
    You need to play around with this to get to grips with it and how to apply it.

  • How to Delete duplicates rows without using rowid/distinct.

    How to delete duplicates rows present in a table without using rowid or even distinct.

    How about:
    SQL> SELECT * FROM t1;
             A          B
             1          2
             2          3
             1          2
             4          4
             4          4
             4          4
    SQL> DELETE FROM t1
      2  WHERE (a, b) IN (SELECT a, b FROM t1
      3                   GROUP BY a, b
      4                   HAVING COUNT(*) > 1) and
      5        rownum = 1;
    1 row deleted.
    SQL> /
    1 row deleted.
    SQL> /
    1 row deleted.
    SQL> /
    0 rows deleted.
    SQL> SELECT * FROM t1;
             A          B
             2          3
             1          2
             4          4Although, if I was asked a similar question with all those restrictions, my first response would be along the lines of: Is this question indicative of the way I will have to work if I join this company? If so, there is no point answering because I wouldn't touch this job with a ten foot pole.

  • How to read personal no's on selection in LDB without using GET PERNR

    Hi to all
    I am using Logical data base PNP and selection screen 900.
    Now my requirement is to retrieve personal no's which are entered at selection seperately without using GET PERNR.
    Ineed all the pernrwhich are entered in selection.
    How to do that.
    Please guide.
    Regards
    Anubhav

    Hi,
    You can do as SUJIT said or use GET PERNR and assign PERN-PERNR to Workarea and then append that to internal table.
    DATA:
       ITAB_PERNR like standard table of PA0000-pernr,
       wa_pernr like line of ITAB_PERNR.
    GET PERNR.
    wa_pernr-pernr  = pernr-pernr.
    append wa_pernr to ITAB_PERNR
    Hope this would help you.
    Regards
    Narin Nandivada.

  • How to delete a JTable row without using its model

    I need to delete a row or insert a row in a JTable without using it's model. The table model for the current JTable extends AbstractTableModel so there are no defined deleteRow or insertRow methods as there are in DefaultTableModel.
    Basically, I don't know if the JTables models are going to have a deleteRow or insertRow method as all the models are AbstractTableModels and might or might not have these methods.
    Thanks.

    I interpreted "I don't have access" as meaning that there isn't a table model for that JTable. For example, you can construct a JTable this way:
    public JTable(Vector rowData, Vector columnNames)
    And the Vector of row data is what you manipulate to add or delete rows and yet you're not using a table model. If that's how your code is written then you would simply manipulate the rowdata Vector.
    However, if you mean that there is a Table model and you're not allowed to change it, then you REALLY need to get permission from your boss to change that table model. Adding a deleteRow(int) method to the TableModel wont break anything that currently exist. And to try to do an end run around the code is a nasty hack. The right way to do it is to add a method to whatever class extends AbstractTableModel and is set as the table model for this JTable.

  • Catch Selection in JFileChooser without using Buttons

    How do you catch a selection in a JFileChooser without using the OK Button?
    If someone clicks on a file(and does not hit enter/cancel buttons), I want to get the file name.
    I tried actionevent but it only works after the user hits a button.

    I'm using Forte and it won't let me apply that listener to a filechooser. So unless I'm missing something, I'm not sure how to implement it or use it correctly

  • Select distinct records without using distinct

    hi experts,
    my retrieved data like these:
    cnt_id cnt_type rcrd_id wrkflw_id
    558848     PRODUCT     553503     248     
    558848     PRODUCT     553503     248     
    558848     PRODUCT     553503     248     
    558808     PRODUCT     553463     248     
    558808     PRODUCT     553463     248     
    558808     PRODUCT     553463     248     
    558810     PRODUCT     553463     248     
    558810     PRODUCT     553463     248     
    558810     PRODUCT     553463     248
    now i want to select one record for each cnt_id without using any distinct function .. how can i do that?
    regards,
    SKP

    you can use the below query
    select * from t where rowid in (select max(rowid)
    from t group by cnt_id)You're query is not the equivalent of DISTINCT.
    SQL> ed
    Wrote file afiedt.buf
      1  create table t as
      2            (select 558848 as cnt_id, 'PRODUCT' as cnt_type, 553503 as rcrd_id, 248 as wrkflw_id from dual union all
      3             select 558848, 'RETURN', 553503, 248 from dual union all
      4             select 558848, 'PRODUCT', 553503, 248 from dual union all
      5             select 558808, 'PRODUCT', 553463, 248 from dual union all
      6             select 558808, 'PRODUCT', 553463, 248 from dual union all
      7             select 558808, 'PRODUCT', 553463, 248 from dual union all
      8             select 558810, 'PRODUCT', 553463, 248 from dual union all
      9             select 558810, 'PRODUCT', 553463, 248 from dual union all
    10*            select 558810, 'PRODUCT', 553463, 248 from dual)
    11  /
    Table created.
    Elapsed: 00:00:00.01
    SQL> select * from t where rowid in (select max(rowid) from t group by cnt_id);
        CNT_ID CNT_TYP    RCRD_ID  WRKFLW_ID
        558810 PRODUCT     553463        248
        558808 PRODUCT     553463        248
        558848 PRODUCT     553503        248
    Elapsed: 00:00:00.00
    SQL> select distinct * from t;
        CNT_ID CNT_TYP    RCRD_ID  WRKFLW_ID
        558810 PRODUCT     553463        248
        558808 PRODUCT     553463        248
        558848 RETURN      553503        248
        558848 PRODUCT     553503        248
    Elapsed: 00:00:00.00
    SQL>

  • Merging multiple rows without using max group by

    Hi guys,
    Is there any better way to convert
    with
    test as
    select '001' as id, 'Apple' as name, 'A' as segment1,null as segment2,null as segment3 from dual union all
    select '001' as id, 'Apple' as name,null as segment1, 'P' as segment2,null as segment3 from dual union all
    select '001' as id, 'Apple' as name,null as segment1,null as segment2, 'L' as segment3 from dual
    select * from test
    to
    select '001' as id, 'Apple' as name, 'A' as segment1,'P' as segment2,'L' as segment3 from dual
    without using max and group by?
    There are about 5 million records and using max group by is slowing it down by a lot.
    It is needed to be done by SQL.
    Thank You

    with
    test as
    select '001' as id, 'Apple' as name, 'A' as segment1,null as segment2,null as segment3 from dual union all
    select '001' as id, 'Apple' as name,null as segment1, 'P' as segment2,null as segment3 from dual union all
    select '001' as id, 'Apple' as name,null as segment1,null as segment2, 'L' as segment3 from dual
    select distinct id, name
    , last_value(segment1 ignore nulls) over (partition by id, name order by segment1) as seg1
    , last_value(segment2 ignore nulls) over (partition by id, name order by segment2) as seg2
    , last_value(segment3 ignore nulls) over (partition by id, name order by segment3) as seg3
    from test
    ;No max or group by, but not necessarily any faster. If you post your actual query, some sample data with expected results, and the additional information requested in the following posts, then someone may be able to provide better suggestions.
    When your query takes too long
    When your query takes too long ...
    How to Post a SQL statement tuning request
    HOW TO: Post a SQL statement tuning request - template posting
    Regards,
    Bob

  • Direct Selection of Carrier without using Optimizer

    Is there any way we can select the carrier without activating the Optimizer setting in SAP TM.
    For example, based on some condition,
    like source location and destination within US, I need to select a carrier automatically
    Any input will be appreciated.

    Can you assign it in the Sales Order? 
    Regards,
    Mike

  • Result set without using cursor in Oracle

    Hi,
    I am very much new to oracle. we have a JDBC code and SQL Server procedure and which works fine. Now that we want to implement the same in Oracle also.we deceided not to change the JDBC but to change Stored Procedure and schema.
    Assume for Eg there is a stored procedure in SQL Server that return a simple query from table
    say
    creat procedure Test
    as
    begin
    select * from Accounts
    return
    end
    This procedure returns all the rows in the Accounts table and corresponding java code the fetch all the rows from ResultSet.
    CallableStatement cStmt = con.prepareCall("{ call test ? }" );
    cStmt.setInt( 1 , id_ );
    ResultSet rs = cStmt.executeQuery();
    while( rs.next() )
    // get all the rows
    will give all the rows from the table account.
    Now that we want to implement the same in Oracle.
    I just want to know, does Oracle provides any way to write a pl/sql or Procedure that returns resultset not as a cursor, but i should work with existing code.
    I don't want to declare like the below code
    CallableStatement cStmt = con.prepareCall("{? = call BrowseAccount.getRefCursor }");
    cStmt.registerOutParameter(1, OracleTypes.CURSOR);
    cStmt.execute();
    ResultSet rs = null;
    rs = (ResultSet)cStmt.getObject(1);
    while (rs.next() )
    // get the resultset
    If anybody have any suggestion or sample code will be really appreciated
    thanks
    vijay

    This is from the Oracle JDBC FAQ at http://otn.oracle.com/tech/java/sqlj_jdbc/htdocs/jdbc_faq.htm#_51_ :
    How do the JDBC drivers support Oracle REFCURSOR datatypes?
    The Oracle JDBC driver supports bind variables of type REFCURSOR. A REFCURSOR is represented by a JDBC ResultSet. Use the getCursor method of the CallableStatement to convert a REFCURSOR value returned by a PL/SQL block into a ResultSet. JDBC lets you call a stored procedure that executes a query and returns a results set. Cast the corresponding CallableStatement to oracle.jdbc.driver.OracleCallableStatement to use the getCursor method.
    Importing classes from the oracle.jdbc.driver package makes programs more readable. Here is a simple example. The samples subdirectory of the distribution has additional examples.
    import oracle.jdbc.driver.*;
    CallableStatement cstmt;
    ResultSet cursor;
    // Use a PL/SQL block to open the cursor
    cstmt = conn.prepareCall
    ("begin open ? for select ename from emp; end;");
    cstmt.registerOutParameter (1, OracleTypes.CURSOR);
    cstmt.execute ();
    cursor = ((OracleCallableStatement)cstmt).getCursor (1);
    // Use the cursor like a normal ResultSet
    while (cursor.next ())
    {System.out.println (cursor.getString (1));}

  • Selecting Multiple dimensions without using Dashboard Prompt

    Hi All,
    We have and requirement where we need to given an option to users where they should be able to select dimensions values to check set of reports in dashboard based on these selected values.
    As we know one way is to have dashboard prompt and all the reports which we need to analyze under this, but in dashboard prompt if we more values selected values will take more time.
    Instead we want in one page all the dimension values will be there and we need some thing like check box option in front of dimension values and based on the dimension values selection, we need to analyze reports available in another dashboard page.
    Thanks in advance.
    Regards,
    Som

    Thanks John for your reply.
    I'm not sure this is going to help me, may be my question is little bit confusing. Let me explain with some example.
    Lets us consider a dashboard page where we have All US States as global filter. Suppose by default global filter will have All values but now every time we want to analyze by selected states now to perform this we need to edit global filter and add States by which we want to analyze.
    By this option we are able to get the result what we want, but problem is that suppose every time we want to analyze by these states only the every time when we login we need to edit global filter again and again. which we don't want to do again and again.
    Now my questions are:-
    1. Is there a way to save values selected into global filter for future use by which we cab avoid selecting values again and again (one way is that we can add defaults in global filter edit mode but can we do this outside.)
    2. Or can we provide in one page just to select the values of analysis like dimensions and next to dimension like check box just to select values and in other pages based of the values selected.
    Regards,
    Somu

Maybe you are looking for

  • Active session Spike on Oracle RAC 11G R2 on HP UX

    Dear Experts, We need urgent help please, as we are facing very low performance in production database. We are having oracle 11G RAC on HP Unix environment. Following is the ADDM report. Kindly check and please help me to figure it out the issue and

  • How to replace obsolete download function module in ECC6.0?

    Hi Experts, How to replace obsolete download function module in ECC6.0? Thanks, Adi.

  • ASR9000/XR - QoS - Limit overall amount of bandwidth per IP

    We have a few thousand broadband customers in our network with various speed packages.  The highest speed package of 500Mbps.  I'm trying to create an ingress QoS policy that I can apply on my interface facing my upstream providers that will limit pu

  • Transforming non-theme data in Mapviewer

    Hi, We have generated a request to display a linestring over the top of a map base. At each end of the linestring are a marker to denote the start and end of the string. The challenge arises when we transform the map base to a desired projection (SRI

  • OBIEE 11g Guided Navigation Link

    Hi All, In 10g I used the guided nav link to link main menu page with hidden dashboard page, so that also presentation variable is passed to the report published on the hidden page. I'm not able to find this on 11g, if I use a link to the hidden page