Simple LIMIT Clause Impossible?

Is the simple LIMIT clause available in other databases simply impossible in Oracle? This is driving me nuts and I'm just not finding a good answer.
For example, the query:
SELECT * FROM MYUSER ORDER BY LOGIN LIMIT 41, 20
This would return up to 20 rows starting at row 41 in the query.
How is this done in Oracle?
I've seen ROWNUM, but:
SELECT * FROM MYUSER ORDER BY LOGIN WHERE ROWNUM > 40 AND ROWNUM < 61
This won't work because ROWNUM > x is not valid and will always return nothing. Also the ORDER BY is done after the where, so you wouldn't get the right rows anyway.
You can fix the ORDER BY problem with:
SELECT * FROM MYUSER (SELECT LOGIN FROM MYUSER ORDER BY LOGIN) WHERE ROWNUM > 40 AND ROWNUM < 61
But you still have the ROWNUM > x is invalid problem.
This seems just as silly as Oracle's NULL == '' problem. But anyway...
Let me explain why I need this and maybe someone can tell me what I can do as a workaround.
I have a web application that (among many other things) has a table of users. This table can easily reach into the tens of thousands of entries.
The web application allows the administrator to page through the list of users, sorting by different criteria, so that they can locate a particular user, group of users, or just so they can generally browse things.
I attempt to keep as little stateful information as possible so that memory (and other resources) aren't wasted.
When a user clicks on the list of users, I do a query for the first 20 rows and display them, sorted by whatever criteria the user sets. The user can then hit the "next page" link and I do another query for rows 21-40 and display those. And so on, both forward and backward, with an option to go to the first 20 and the last 20.
I'm doing all this through Java and JDBC on a proprietary web server that supports Java servlets.
I could keep a result set open and that would help with page forwards (just grab the next 20 from the set). But what about paging backward? Or jumping ahead to the end? Or jumping ahead or backward several pages?
This seems like a very common web application theme. What am I missing?
Greg
null

To answer your last question ('What am I missing?') first - you aren't missing anything.
You are correct - this is a very common question. You will find a dozen or more similar questions if you search this forum (search on ROWNUM).
Oracle assigns ROWNUM as the result set records are being retrieved. Naturally this is before the ORDER BY clause since Oracle can't order what doesn't exist.
That is why 'ROWNUM <' works (oracle quits when the desired number of rows is reached) but 'ROWNUM >' does not work (ROWNUM will never get to the desired number).
The trick is to use a nested query or multiple nested queries. Oracle 8i supports the ORDER BY clause in nested queries.
The first (innermost) query selects records and orders them.
The second query is used to wrap the first query and assign row numbers.
The third query selects the desired range of rows from the second query.
'select * from (select rownum rn, e.* from
(select * from emp order by ename desc) e) where rn >= 4'
If an ORDER BY is not needed you can eliminate the first query.
'select * from (select rownum rn, e.* from emp e) where rn >= 4'
The inner query creates the RN column based on ROWNUM and the outer query uses the RN column (just as if it existed in a table) to select records with an RN value >= 4.
Keep in mind that the performance will go down as the number of records goes up.
For best results you need have a column in the table that you can use in the WHERE clause. Unfortunately this isn't always possible if you are trying to support generic ORDER BY clauses.
One way that is often useful to try to understand why things like this 'are the way they are' is to examine how you would perform the task manually.
Suppose I place in front of you a stack of 8 1/2 X 11 paper whose pages are not numbered and ask you to find and make copies of pages 40 - 60.
How would you do it? You would start counting from the beginning. When you reach page 40 you would copy the next 20 pages.
When you are done the pile of paper is in exactly the same state as when you started.
Now I ask you to give me copies of pages 61 to 80.
You have to start all over at the beginning as if the first request never happened.
That's pretty much what the computer has to do.
If the pages are numbered you can go directly to the desired pages.
Now what if I ask you to copy pages 20 to 30 after you sort them in order by LAST NAME?
There are exceptions (e.g. parallel processing, neural networks) but generally the computer can only do what you can do yourself. It can just do it faster and more efficiently.
Certain types of problems are not well-suited for the computer and some problems cannot be solved at all.
null

Similar Messages

  • LIMIT clause

    When I used MS Access with sql language I remender that for limiting retrieved records of a query I could use:
    SELECT TOP 1 * from TABLE_NAME
    So I searched for a similar clause for oracle queries and someone pointed me the LIMIT clause.
    Could anyone gimme an example on how to use it?

    The rownum usage can answered to the OP question, depend of what exactly he wants... but like below seems a little better :
    SCOTT@demo102> select empno, ename, sal, deptno from emp;
         EMPNO ENAME             SAL     DEPTNO
          7369 SMITH            8000         20
          7499 ALLEN           16000         30
          7521 WARD            12500         30
          7566 JONES           29750         20
          7654 MARTIN          12500         30
          7698 BLAKE           28500         30
          7782 CLARK           24500         10
          7788 SCOTT           30000         20
          7839 KING            50000         10
          7844 TURNER          15000         30
          7876 ADAMS           11000         20
          7900 JAMES            9500         30
          7902 FORD            30000         20
          7934 MILLER          13000         10
    14 rows selected.
    SCOTT@demo102> select empno, ename, sal, deptno
      2            from (select empno, ename, sal, deptno from emp order by sal desc)
      3            where rownum <=5;
         EMPNO ENAME             SAL     DEPTNO
          7839 KING            50000         10
          7788 SCOTT           30000         20
          7902 FORD            30000         20
          7566 JONES           29750         20
          7698 BLAKE           28500         30
    SCOTT@demo102> Anyway, like I said in the link given earlier, there is rank, dense_rank which can help for different requirements.
    Nicolas.

  • MySQL LIMIT clause equivalent in ORACLE statement

    Is there an Oracle SQL statement that would be equivalent to the MySQL LIMIT clause used to return only part of a result set?

    The preferred solution is to make use of the analytic function ROW_NUMBER. There is a description in the Oracle documentation. The final example shown returns only the fifty-first through one-hundredth row of the EMPLOYEES table:
    SELECT last_name FROM
       (SELECT last_name, ROW_NUMBER() OVER (ORDER BY last_name) R FROM employees)
       WHERE R BETWEEN 51 and 100;An alternative is to use:
        select *
          from ( select a.*, rownum rnum
                from ( YOUR_QUERY_GOES_HERE -- including the order by ) a
               where rownum <= MAX_ROWS )
         where rnum >= MIN_ROWS;which is discussed on asktom.oracle.com. This works from Oracle 8.1 onwards.
    -- cj

  • Jet OLEDB 'LIMIT' clause

    Hi Folks,
    I'm monitoring an access database for new records using LV2009 + the Database connectivity toolset(not 8.5.1 as in sig). Anyway the number of rows could be on the order of 200,000+, so I want to limit my query to only one record, the last. Unable to get the LIMIT clause to function, I searched and came across this: http://office.microsoft.com/en-us/access/HP010322501033.aspx. It states:
    Microsoft Jet SQL does not support the following ANSI SQL features:
    DISTINCT aggregate function references. For example, Microsoft Jet SQL does not allow SUM(DISTINCT columnname).
    The LIMIT TO nn ROWS clause used to limit the number of rows returned by a query. You can use only the WHERE Clause to limit the scope of a query.
    Is this the problem I'm going up against? The reason I use Jet is because I can more easily programatically load individual databases (and not have to mess around with UDL files).
    Any thoughts on a work-around?
    Happy Wiring,
    Jamie
    v2009 devel. w/RT
    Attachments:
    DB-test.zip ‏27 KB

    Potential workaround: may not be the most efficient solution though (my only exp. w/database access is from PHP/mySQL many years back!). Comments welcome. :-)
    v2009 devel. w/RT
    Attachments:
    DB-access-TEST_v2.vi ‏25 KB

  • At 60 and totally confused w/version . Not  clue with how to use it and how to change it. My question - If a new version of i-tunes can be offered why not the older version as being a choice? Too simple or just impossible to do? I hate to drop i-tunes.

    At 60 years of age and totally challenged by todays technology I find it impossible to understand i-tunes Version 11. Don' ask me to try to try go back to a earlier version ( as much as I would like to). I have read some of the advice given, as well as intended as it is, I don't understand the process. I don't want to drop i-tune but I will. I am sure my library is not as large as many others who use it but it is just as important to me. This really saddens me.
    I do have two question if some one can help me. First, someone mentioned that if I go to previous restore point this might correct it. Yes or no? Secondly, if Apple can offer us a new version to download why can't they offer an older version to download. Too simple or impossible to do.

    You can still drag and drop a song into a Playlist, in the same manner as before. Simply use the left-click of your mouse to select-and-hold the song (left-click-and-hold, don't release the click) and drag it to and then drop it (release the click) into the Playlist you want. (You cannot drop it into a Smart Playlist, you can only drop it into a Regular Playlist.)
    ... and here's a screenshot dragging the song "Lazy Orbit" into the Regular Playlist "Sat 4th June 2011". A small + symbol also appears in the drag, but it doesn't show up on screenshots. If the move is forbidden, a red slash through a red circle appears instead of the + symbol.
    As for the Albums thing; in the Column Browser, right-click anywhere in the Column Browser header (see my screenshots in my previous post) and a menu appears. Deselect Albums on that menu to remove Albums from the upper Columns:
    ... and for the columns underneath (with the songs list etc.), right-click on that header bar and once again, deselect Albums from the menu that appears. The Albums column will then be removed from the lower columns list.
    ed294 wrote:
    You would think with all the complaints they are getting they would want us, the buyers, to be satisfied with there product.
    Well, my advice to you is: do not think about what you think iTunes cannot do, instead, think about what you want to achieve - and then work out how to achieve it.

  • JPQL: MySQL-like LIMIT-clause

    hi
    i was wondering if i could possibly limit a JPQL result list in the way that it only contains a specified number of rows starting at a certain offset.
    for example, in MySQL i could simply use LIMIT like this:
    SELECT * FROM example ORDER BY c1 LIMIT 20, 10
    and it would return 10 rows starting at 20th row instead of the whole realtion.
    any help will be appreciated

    The preferred solution is to make use of the analytic function ROW_NUMBER. There is a description in the Oracle documentation. The final example shown returns only the fifty-first through one-hundredth row of the EMPLOYEES table:
    SELECT last_name FROM
       (SELECT last_name, ROW_NUMBER() OVER (ORDER BY last_name) R FROM employees)
       WHERE R BETWEEN 51 and 100;An alternative is to use:
        select *
          from ( select a.*, rownum rnum
                from ( YOUR_QUERY_GOES_HERE -- including the order by ) a
               where rownum <= MAX_ROWS )
         where rnum >= MIN_ROWS;which is discussed on asktom.oracle.com. This works from Oracle 8.1 onwards.
    -- cj

  • WHAT IS IT ABOUT THIS VERSION WHICH IS MAKING A SIMPLE TASK SO IMPOSSIBLE??

    Frankly quicktime 7 vs newest is making all movie work and viewing a nightmare. I have installed and reinstalled QT 4 times and the codecs etc which I have spent a lot of $$ on. Still having problems with FLV and more. IF ANYONE has any idea what to do or what not to do please PLEASE advise. I am using QT Pro the latest version and by the way - all was 10000000 percent until this *T&^ version appeared.
    Thanks!
    sandz
    macbook dual core Mac OS X (10.4.9)
    macbook   Mac OS X (10.4.9)  

    thanks for that rather tongue in cheek reply!!
    I do have perian up and running (for a while now and I do export to MPEG.
    With QT pro this is not a problem until the latest verion especially when creating streaming vids and publishing.
    I will avoid all logs!
    thank you!

  • Artificial limit of 10 numbers for call block.

    Why is call block limited to 10 numbers? There are at least that many scammers calling every week. I need to be able to block several thousand numbers. This should be incredibly simple with any modern computing equipment at least since about 1970. There is no reason whatsoever for a limitation of 10 numbers, unless the telephone company is profiting from all of these illegal calls. (I'm sure that's not the case, of course.) There should also be a feature for blocking known scammers. For example, when a scammer calls, I could just enter *XX and that number would be added to the list of known scammers, and be blocked for everyone who has that feature turned on. That number could then be blocked for everyone for a day. That would very quickly stop the numerous telephone scams, as they would only be able to complete one call per day. It would be a simple matter to have a "whitelist" of good numbers that can always ring my phone just in case someone has mistakenly labelled them as a scammer.

    Kestrel wrote:
    Agree - 10 is not enough.  There should not be a limit at all.
    Just want to say that no limit is impossible.  Say they gave us 1 million numbers someone would find they needed more.  Better would be they worked with someone like NOMOROBO to filter numbers for all of us and gave us say some number like 100 of personal numbers to filter.  Note you must use personal numbers to filter those allowed to annoy us such as politicians during campaign season  Note with NOMOROBO you cannot add personal numbers but you can report a new violator to filter.

  • Use of DECODE and NULL value in a WHERE clause

    hi all,
    I came into an issue trying to use the DECODE function in a simple where clause.
    this is my test case
    CREATE TABLE tab_test (lev NUMBER, code VARCHAR2(10), val VARCHAR2(10));
    INSERT INTO tab_test VALUES (1, NULL, 'val11');
    INSERT INTO tab_test VALUES (1, NULL, 'val12');
    INSERT INTO tab_test VALUES (1, '13', 'val13');
    INSERT INTO tab_test VALUES (2, '21', 'val21');
    INSERT INTO tab_test VALUES (1, '22', 'val22');
    INSERT INTO tab_test VALUES (1, '23', 'val23');
    and this is the query
    SELECT * FROM tab_test WHERE code = DECODE(:lev,1,NULL,:cod)
    as you can see running this query, setting :lev to 1 will return an empty record set, instead of the expected first two rows (as it will be, running "select * from tab_test where code is null").
    is there a way to overcome this issue? thanks for help

    hi Frank,
    I can confirm that the queries do work when run on TOAD, but do not when they are put in the WHERE clause of my data block. I guess the problem relies on some data block setting; I should investigate on Forms forum.
    Anyway, here is my test case, including data and some examples. You can realize that it works good
    CREATE TABLE TAB_TEST
    LEV NUMBER,
    CODE VARCHAR2(10 BYTE),
    VAL VARCHAR2(10 BYTE)
    Insert into tab_test
    (LEV, CODE, VAL)
    Values
    (3, 'val21', 'val3_211');
    Insert into tab_test
    (LEV, CODE, VAL)
    Values
    (2, 'val21', 'val3_212');
    Insert into tab_test
    (LEV, VAL)
    Values
    (1, 'val11');
    Insert into tab_test
    (LEV, VAL)
    Values
    (1, 'val12');
    Insert into tab_test
    (LEV, VAL)
    Values
    (1, 'val13');
    Insert into tab_test
    (LEV, CODE, VAL)
    Values
    (2, 'val11', 'val21');
    Insert into tab_test
    (LEV, CODE, VAL)
    Values
    (2, 'val12', 'val22');
    Insert into tab_test
    (LEV, CODE, VAL)
    Values
    (2, 'val13', 'val23');
    COMMIT;
    SELECT * FROM TAB_TEST
    CONNECT
    BY PRIOR val = code
    START WITH (CASE
    WHEN :LIV = 1 AND code IS NULL THEN 'OK'
    WHEN :LIV != 1 AND code = :COD THEN 'OK'
    END = 'OK')
                   AND VAL = :V
    :LIV = 1
    :COD = [any]
    :V = 'val11'
    LEV     CODE     VAL
    1          val11
    2     val11     val21
    3     val21     val3_211
    3     val21     val3_212
    :LIV = 1
    :COD = [any]
    :V = 'val12'
    LEV     CODE     VAL
    1          val12
    2     val12     val22
    :LIV = 2
    :COD = 'val11'
    :V = 'val21'
    LEV     CODE     VAL
    2     val11     val21
    3     val21     val3_211
    2     val21     val3_212

  • Bulk collect limit 1000 is looping only 1000 records out of 35000 records

    In below code I have to loop around 35000 records for every month of the year starting from Aug-2010 to Aug-2011.
    I am using bulk collect with limit clause but the problem is:
    a: Limit clause is returning only 1000 records.
    b: It is taking too much time to process.
    CREATE OR REPLACE PACKAGE BODY UDBFINV AS
    F UTL_FILE.FILE_TYPE;
    PV_SEQ_NO NUMBER(7);
    PV_REC_CNT NUMBER(7) := 0;
    PV_CRLF VARCHAR2(2) := CHR(13) || CHR(10);
    TYPE REC_PART IS RECORD(
    PART_NUM PM_PART_HARSH.PART_NUM%TYPE,
    ON_HAND_QTY PM_PART_HARSH.ON_HAND_QTY%TYPE,
    ENGG_PREFIX PM_PART_HARSH.ENGG_PREFIX%TYPE,
    ENGG_BASE PM_PART_HARSH.ENGG_BASE%TYPE,
    ENGG_SUFFIX PM_PART_HARSH.ENGG_SUFFIX%TYPE);
    TYPE TB_PART IS TABLE OF REC_PART;
    TYPE REC_DATE IS RECORD(
    START_DATE DATE,
    END_DATE DATE);
    TYPE TB_MONTH IS TABLE OF REC_DATE;
    PROCEDURE MAIN IS
    /* To be called in Scheduler Programs Action */
    BEGIN
    /* Initializing package global variables;*/
    IFMAINT.V_PROG_NAME := 'FULL_INVENTORY';
    IFMAINT.V_ERR_LOG_TAB := 'UDB_ERR_FINV';
    IFMAINT.V_HIST_TAB := 'UDB_HT_FINV';
    IFMAINT.V_UTL_DIR_NAME := 'UDB_SEND';
    IFMAINT.V_PROG_TYPE := 'S';
    IFMAINT.V_IF_TYPE := 'U';
    IFMAINT.V_REC_CNT := 0;
    IFMAINT.V_DEL_INS := 'Y';
    IFMAINT.V_KEY_INFO := NULL;
    IFMAINT.V_MSG := NULL;
    IFMAINT.V_ORA_MSG := NULL;
    IFSMAINT.V_FILE_NUM := IFSMAINT.V_FILE_NUM + 1;
    IFMAINT.LOG_ERROR; /*Initialize error log table, delete prev. rows*/
    /*End of initialization section*/
    IFMAINT.SET_INITIAL_PARAM;
    IFMAINT.SET_PROGRAM_PARAM;
    IFMAINT.SET_UTL_DIR_PATH;
    IFMAINT.GET_DEALER_PARAMETERS;
    PV_SEQ_NO := IFSMAINT.GENERATE_FILE_NAME;
    IF NOT CHECK_FILE_EXISTS THEN
    WRITE_FILE;
    END IF;
    IF IFMAINT.V_BACKUP_PATH_SEND IS NOT NULL THEN
    IFMAINT.COPY_FILE(IFMAINT.V_UTL_DIR_PATH,
    IFMAINT.V_FILE_NAME,
    IFMAINT.V_BACKUP_PATH_SEND);
    END IF;
    IFMAINT.MOVE_FILE(IFMAINT.V_UTL_DIR_PATH,
    IFMAINT.V_FILE_NAME,
    IFMAINT.V_FILE_DEST_PATH);
    COMMIT;
    EXCEPTION
    WHEN IFMAINT.E_TERMINATE THEN
    IFMAINT.V_DEL_INS := 'N';
    IFMAINT.LOG_ERROR;
    ROLLBACK;
    UTL_FILE.FCLOSE(F);
    IFMAINT.DELETE_FILE(IFMAINT.V_UTL_DIR_PATH, IFMAINT.V_FILE_NAME);
    RAISE_APPLICATION_ERROR(IFMAINT.V_USER_ERRCODE, IFMAINT.V_ORA_MSG);
    WHEN OTHERS THEN
    IFMAINT.V_DEL_INS := 'N';
    IFMAINT.V_MSG := 'ERROR IN MAIN PROCEDURE ||IFMAINT.V_PROG_NAME';
    IFMAINT.V_ORA_MSG := SUBSTR(SQLERRM, 1, 255);
    IFMAINT.V_USER_ERRCODE := -20101;
    IFMAINT.LOG_ERROR;
    ROLLBACK;
    UTL_FILE.FCLOSE(F);
    IFMAINT.DELETE_FILE(IFMAINT.V_UTL_DIR_PATH, IFMAINT.V_FILE_NAME);
    RAISE_APPLICATION_ERROR(IFMAINT.V_USER_ERRCODE, IFMAINT.V_ORA_MSG);
    END;
    PROCEDURE WRITE_FILE IS
    CURSOR CR_PART IS
    SELECT A.PART_NUM, ON_HAND_QTY, ENGG_PREFIX, ENGG_BASE, ENGG_SUFFIX
    FROM PM_PART_HARSH A;
    lv_cursor TB_PART;
    LV_CURR_MONTH NUMBER;
    LV_MONTH_1 NUMBER := NULL;
    LV_MONTH_2 NUMBER := NULL;
    LV_MONTH_3 NUMBER := NULL;
    LV_MONTH_4 NUMBER := NULL;
    LV_MONTH_5 NUMBER := NULL;
    LV_MONTH_6 NUMBER := NULL;
    LV_MONTH_7 NUMBER := NULL;
    LV_MONTH_8 NUMBER := NULL;
    LV_MONTH_9 NUMBER := NULL;
    LV_MONTH_10 NUMBER := NULL;
    LV_MONTH_11 NUMBER := NULL;
    LV_MONTH_12 NUMBER := NULL;
    lv_month TB_MONTH := TB_MONTH();
    BEGIN
    IF CR_PART%ISOPEN THEN
    CLOSE CR_PART;
    END IF;
    FOR K IN 1 .. 12 LOOP
    lv_month.EXTEND();
    lv_month(k).start_date := ADD_MONTHS(TRUNC(SYSDATE, 'MM'), - (K + 1));
    lv_month(k).end_date := (ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -K) - 1);
    END LOOP;
    F := utl_file.fopen(IFMAINT.V_UTL_DIR_NAME, IFMAINT.V_FILE_NAME, 'W');
    IF UTL_FILE.IS_OPEN(F) THEN
    /*FILE HEADER*/
    utl_file.put_line(F,
    RPAD('$CUD-', 5, ' ') ||
    RPAD(SUBSTR(IFMAINT.V_PANDA_CD, 1, 5), 5, ' ') ||
    RPAD('-136-', 5, ' ') || RPAD('000000', 6, ' ') ||
    RPAD('-REDFLEX-KA-', 13, ' ') ||
    RPAD('00000000-', 9, ' ') ||
    RPAD(IFMAINT.V_CDS_SPEC_REL_NUM, 5, ' ') ||
    RPAD('CD', 2, ' ') ||
    RPAD(TO_CHAR(SYSDATE, 'MMDDYY'), 6, ' ') ||
    LPAD(IFSMAINT.V_FILE_NUM, 2, 0) ||
    RPAD('-', 1, ' ') || RPAD(' ', 9, ' ') ||
    RPAD('-', 1, ' ') || RPAD(' ', 17, ' ') ||
    RPAD('CD230', 5, ' ') ||
    RPAD(TO_CHAR(SYSDATE, 'MMDDYY'), 6, ' ') ||
    LPAD(IFSMAINT.V_FILE_NUM, 2, 0) ||
    LPAD(PV_REC_CNT, 8, 0) || RPAD(' ', 5, ' ') ||
    RPAD('00000000', 8, ' ') || RPAD('CUD', 3, ' ') ||
    RPAD(IFMAINT.V_CDS_SPEC_REL_NUM, 5, ' ') ||
    RPAD(IFMAINT.V_GEO_SALES_AREA_CD, 3, ' ') ||
    RPAD(IFMAINT.V_FRANCHISE_CD, 2, ' ') ||
    RPAD(IFMAINT.V_DSP_REL_NUM, 9, ' ') ||
    RPAD('00136REDFLEX', 12, ' ') || RPAD(' ', 1, ' ') ||
    RPAD('KA', 2, ' ') || RPAD('000000', 6, ' ') ||
    RPAD('00D', 3, ' ') ||
    RPAD(IFMAINT.V_VENDOR_ID, 6, ' ') ||
    RPAD(IFSMAINT.V_FILE_TYPE, 1, ' ') ||
    RPAD('>', 1, ' ') || PV_CRLF);
    /*LINE ITEMS*/
    OPEN CR_PART;
    FETCH CR_PART BULK COLLECT
    INTO lv_cursor limit 1000;
    FOR I IN lv_cursor.FIRST .. lv_cursor.LAST LOOP
    SELECT SUM(A.BILL_QTY)
    INTO LV_CURR_MONTH
    FROM PD_ISSUE A, PH_ISSUE B
    WHERE A.DOC_TYPE IN ('CRI', 'RRI', 'RSI', 'CSI')
    AND A.DOC_NUM = B.DOC_NUM
    AND B.DOC_DATE BETWEEN TRUNC(SYSDATE, 'MM') AND SYSDATE
    AND A.PART_NUM = LV_CURSOR(i).PART_NUM;
    FOR J IN 1 .. 12 LOOP
    SELECT SUM(A.BILL_QTY)
    INTO LV_MONTH_1
    FROM PD_ISSUE A, PH_ISSUE B
    WHERE A.DOC_TYPE IN ('CRI', 'RRI', 'RSI', 'CSI')
    AND A.DOC_NUM = B.DOC_NUM
    AND B.DOC_DATE BETWEEN lv_month(J).start_date and lv_month(J)
    .end_date
    AND A.PART_NUM = LV_CURSOR(i).PART_NUM;
    END LOOP;
    utl_file.put_line(F,
    RPAD('IL', 2, ' ') ||
    RPAD(TO_CHAR(SYSDATE, 'RRRRMMDD'), 8, ' ') ||
    RPAD(LV_CURSOR(I).ENGG_PREFIX, 6, ' ') ||
    RPAD(LV_CURSOR(I).ENGG_BASE, 8, ' ') ||
    RPAD(LV_CURSOR(I).ENGG_SUFFIX, 6, ' ') ||
    LPAD(LV_CURSOR(I).ON_HAND_QTY, 7, 0) ||
    LPAD(NVL(LV_CURR_MONTH, 0), 7, 0) ||
    LPAD(LV_MONTH_1, 7, 0) || LPAD(LV_MONTH_2, 7, 0) ||
    LPAD(LV_MONTH_3, 7, 0) || LPAD(LV_MONTH_4, 7, 0) ||
    LPAD(LV_MONTH_5, 7, 0) || LPAD(LV_MONTH_6, 7, 0) ||
    LPAD(LV_MONTH_7, 7, 0) || LPAD(LV_MONTH_8, 7, 0) ||
    LPAD(LV_MONTH_9, 7, 0) || LPAD(LV_MONTH_10, 7, 0) ||
    LPAD(LV_MONTH_11, 7, 0) ||
    LPAD(LV_MONTH_12, 7, 0));
    IFMAINT.V_REC_CNT := IFMAINT.V_REC_CNT + 1;
    END LOOP;
    CLOSE CR_PART;
    /*TRAILER*/
    utl_file.put_line(F,
    RPAD('$EOF-', 5, ' ') || RPAD('320R', 4, ' ') ||
    RPAD(SUBSTR(IFMAINT.V_PANDA_CD, 1, 5), 5, ' ') ||
    RPAD(' ', 5, ' ') ||
    RPAD(IFMAINT.V_GEO_SALES_AREA_CD, 3, ' ') ||
    RPAD(TO_CHAR(SYSDATE, 'MM-DD-RR'), 6, ' ') ||
    LPAD(IFSMAINT.V_FILE_NUM, 2, 0) ||
    LPAD(IFMAINT.V_REC_CNT, 8, 0) || 'H' || '>' ||
    IFMAINT.V_REC_CNT);
    utl_file.fclose(F);
    IFMAINT.INSERT_HISTORY;
    END IF;
    END;
    FUNCTION CHECK_FILE_EXISTS RETURN BOOLEAN IS
    LB_FILE_EXIST BOOLEAN := FALSE;
    LN_FILE_LENGTH NUMBER;
    LN_BLOCK_SIZE NUMBER;
    BEGIN
    UTL_FILE.FGETATTR(IFMAINT.V_UTL_DIR_NAME,
    IFMAINT.V_FILE_NAME,
    LB_FILE_EXIST,
    LN_FILE_LENGTH,
    LN_BLOCK_SIZE);
    IF LB_FILE_EXIST THEN
    RETURN TRUE;
    END IF;
    RETURN FALSE;
    EXCEPTION
    WHEN OTHERS THEN
    RETURN FALSE;
    END;
    END;

    Try this:
    OPEN CR_PART;
    loop
    FETCH CR_PART BULK COLLECT
    INTO lv_cursor limit 1000;
    exit when CR_PART%notfound;
    FOR I IN lv_cursor.FIRST .. lv_cursor.LAST LOOP
    SELECT SUM(A.BILL_QTY)
    INTO LV_CURR_MONTH
    FROM PD_ISSUE A, PH_ISSUE B
    WHERE A.DOC_TYPE IN ('CRI', 'RRI', 'RSI', 'CSI')
    AND A.DOC_NUM = B.DOC_NUM
    AND B.DOC_DATE BETWEEN TRUNC(SYSDATE, 'MM') AND SYSDATE
    AND A.PART_NUM = LV_CURSOR(i).PART_NUM;
    FOR J IN 1 .. 12 LOOP
    SELECT SUM(A.BILL_QTY)
    INTO LV_MONTH_1
    FROM PD_ISSUE A, PH_ISSUE B
    WHERE A.DOC_TYPE IN ('CRI', 'RRI', 'RSI', 'CSI')
    AND A.DOC_NUM = B.DOC_NUM
    AND B.DOC_DATE BETWEEN lv_month(J).start_date and lv_month(J)
    .end_date
    AND A.PART_NUM = LV_CURSOR(i).PART_NUM;
    END LOOP;
    utl_file.put_line(F,
    RPAD('IL', 2, ' ') ||
    RPAD(TO_CHAR(SYSDATE, 'RRRRMMDD'), 8, ' ') ||
    RPAD(LV_CURSOR(I).ENGG_PREFIX, 6, ' ') ||
    RPAD(LV_CURSOR(I).ENGG_BASE, 8, ' ') ||
    RPAD(LV_CURSOR(I).ENGG_SUFFIX, 6, ' ') ||
    LPAD(LV_CURSOR(I).ON_HAND_QTY, 7, 0) ||
    LPAD(NVL(LV_CURR_MONTH, 0), 7, 0) ||
    LPAD(LV_MONTH_1, 7, 0) || LPAD(LV_MONTH_2, 7, 0) ||
    LPAD(LV_MONTH_3, 7, 0) || LPAD(LV_MONTH_4, 7, 0) ||
    LPAD(LV_MONTH_5, 7, 0) || LPAD(LV_MONTH_6, 7, 0) ||
    LPAD(LV_MONTH_7, 7, 0) || LPAD(LV_MONTH_8, 7, 0) ||
    LPAD(LV_MONTH_9, 7, 0) || LPAD(LV_MONTH_10, 7, 0) ||
    LPAD(LV_MONTH_11, 7, 0) ||
    LPAD(LV_MONTH_12, 7, 0));
    IFMAINT.V_REC_CNT := IFMAINT.V_REC_CNT + 1;
    END LOOP;
    end loop;
    CLOSE CR_PART;

  • Beginner: Getting syntax error on WHERE clause in SELECT

    I'm very new to php and mySQL.  Am using DW master/detail to generate to basic code I need.  One thing I need to do is modify a select statement in the master to include a WHERE clause to limit the selection to a particular value in one field.
    I'm getting a syntax error with the WHERE clause I'm adding to the map select statement.
    This is the portion of the error message showing the error location:
    'WHERE Group='Community' LIMIT 0, 10'
    The php that generated the select is:
    $query_maps = "SELECT * FROM tblmaps ORDER BY tblmaps.DispSeq";
    $query_limit_maps = sprintf("%s WHERE Group='%s' LIMIT %d, %d", $query_maps, $selectGroup, $startRow_maps, $maxRows_maps);
    This approach to creating the select statement is from the code generated for the master page.  It adds the LIMIT clause.  All I did was add the "WHERE Group='%s' and the $selectGroup variable which comes from earlier code.  You can see that the $selectGroup variable is equal to the "Community: group.
    I've scanned the web to see what syntax error I might be making but haven't found anything that explains it.
    The full resolved select statement is:
    SELECT * FROM tblmaps ORDER BY tblmaps.DispSeq WHERE Group='Community' LIMIT 0,10
    What am I not seeing?
    Tom

    Thanks.  Make sense but changing that didn't help.
    Here's the error message I'm getting:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Group='Community' ORDER BY tblmaps.DispSeq LIMIT 0, 10' at line 1
    The full select (from a debugging ECHO I inserted) is:
    SELECT * FROM tblmaps WHERE Group='Community' ORDER BY tblmaps.DispSeq LIMIT 0, 10
    Note that when I take the WHERE clause out, there is no syntax error.

  • Limit on result

    I have seen this question around the net several times, but
    no proper solution have been provided.
    What I am doing is that I am basically returning the last 5
    results in the db. The db. has a potential for a size about
    1000 rows.
    Even though by the use of caching etc. I refuse to generate
    a query that returns all the rows, and then selecting 5.
    both MySql and Postgresql has the 'LIMIT' clause, which is
    basically what I want.
    I tried hacking around using ROWID, but this did not generate
    proper results, because the ROWID is generated befor my order
    by clause.
    PLEASE HELP

    You can limit your result by an SQL trick!
    Here is an example.
    SELECT * FROM (SELECT COL1,COL2,ROWNUM ROWNUMBER FROM SOMETABLE
    WHERE SOMECONDITION) WHERE ROWNUMBER BETWEEN 500 AND 600
    PS:the inner SELECT statement is your original query!

  • Problem in a inner query- order by clause

    hi...
    I have a update statement with a simple select clause present as inner query..
    (select col1 from table1 where col2='abc' and rownum=1 order by col3 desc)
    since it is a inner query, thats why i can not remove the brackets.
    col3 may be 0 or 1(1 can occur only once...0 can be multiple times)
    my target is to fetch the record if the col3 is 1
    if it is not 1, then it will fetch the first record with col3=0...thats why i have put order by col3.
    moreover i want only one record, thats why i have put rownum=1.
    but it is failing with 'missing right parenthesis'.
    please help..

    Hi,
    Remember that the ORDER BY clause is applied last, after the WHERE clause is completed, so when you way
    WHERE     ROWNUM = 1
    ORDER BY  col3    DESCin the same sub-query, you're picking one row (arbitrarily), and then "sorting" that one row.
    Here's one way to get the results you want:
         SELECT     col1
         FROM     (
                   SELECT  col1
                   ,     ROW_NUMBER () OVER (ORDER BY  col3  DESC)
                                  AS r_num
                   FROM     table1
                   WHERE     col2     = 'abc'
                   AND     col3     IN (0, 1)
         WHERE     r_num     = 1
    )Depending on how this is used in your complete query, there may be better ways to get the same results.

  • Group by, setting where clause in View Objects

    Hello,
    I have a problem with View Objects in BC4j. Cause I cannot use a column in the where clause of a view object that is not also an attribute of the view object, I am unable to group a statement as I want to. Is there a way to solve this problem?
    Thanks in advance!
    Britta

    What version are you using..I tried a simple WHERE clause using an attribute which is not part of the VO and that works ok....
    Thanks
    Grant Ronald
    Oracle Product Management

  • Can't understand how this group by clause works

    The Schema is as below: (http://www.psoug.org/reference/rollup.html)
    CREATE TABLE grp_rep (
    person_id NUMBER(3),
    division VARCHAR2(3),
    commission NUMBER(5));
    INSERT INTO grp_rep VALUES (1,'SAM',1000);
    INSERT INTO grp_rep VALUES (2,'EUR',1200);
    INSERT INTO grp_rep VALUES (1,'EUR',1450);
    INSERT INTO grp_rep VALUES (1,'EUR',700);
    INSERT INTO grp_rep VALUES (2,'SEA',1000);
    INSERT INTO grp_rep VALUES (2,'SEA',2000);
    INSERT INTO grp_rep VALUES (1,'EUR',800);
    COMMIT;
    Query1:
    SELECT person_id, division, SUM(commission)
    FROM grp_rep
    GROUP BY person_id, ROLLUP (person_id, division);
    Query2:
    SELECT person_id, division, SUM(commission)
    FROM grp_rep
    GROUP BY division, ROLLUP (person_id, division);
    The results of query1 are okay. It has results from rollup and group by person_id.
    But, in Query2 results of rollup are missing and results of group by division is there.
    Anyone can explain how the group by clause works when there are multiple columns involving CUBE, ROLLUP and simple column names?
    Regards.

    Thank you shoblock!
    but, What i m really looking for is,
    How group by clause works when i add regular column along with RollUp in group by clause?
    I have understood simple group by clause like
    ...group by column1,column2,column3....
    n I also know simple rollup clauses like
    ...group by rollup(column1,column2,column3,...)
    But my Problem is how does this work:
    ...group by column2,rollup(column1,column2,...)
    ...group by column1,rollup(column1,column2,...)
    See below Results:
    Query1:
    SELECT person_id, division, SUM(commission)
    FROM grp_rep
    GROUP BY person_id,ROLLUP ( person_id, division );
    Result:
    PERSON_ID DIVISION SUM(COMMISSION)
         1      EUR      2950
         1     SAM      1000
         2      EUR      1200
         2      SEA      3000
         1           3950
         2           4200
         1           3950
         2           4200
    SELECT person_id, division, SUM(commission)
    FROM grp_rep
    GROUP BY division,ROLLUP ( person_id, division );
    Query2:
    SELECT person_id, division, SUM(commission)
    FROM grp_rep
    GROUP BY division,ROLLUP ( person_id, division );
    Result:
    PERSON_ID DIVISION SUM(COMMISSION)
    1 EUR 2950
    2 EUR 1200
         1 SAM      1000
    2 SEA 3000
    1 EUR 2950
    2 EUR 1200
    1 SAM 1000
    2 SEA 3000
    EUR 4150
    SAM 1000
    SEA 3000
    guys, help me make understand above results!
    Regards.

Maybe you are looking for

  • How do I add a video clip after my music?

    Its sort of a wierd question but basically to me imovie09 is a bust since i cant really see a real timeline to edit my project. But what im trying to do is after i edited my video and the song i have ends i want to add another clip after the song but

  • HT201272 songs not showing up in itunes purchased online.

    I am unable to see previous purchased items in the iTunes purchases area. Also when I have found the songs that I have previously purchased and clicked on them to download them agin apple charged me a second time.

  • I should create a new database link to an AS/400 server but i m rookie!!

    İ have an oracle 10g DB server.İ need get some of data from AS/400 DB on other location and put them all to my oracle server.İ should create a database link between oracle server and AS400 DB but how will i realize that event.in oracle 10g enterprise

  • Photoshop CS6 - can no longer save file

    Hello, can save 6 out of file, program crashes in Photoshop CS. Error Message in Windows protocol: Name of misapplication: Photoshop.exe, Version: 13.0.1.34, time stamp: 0x5269b25c Name of the faulty module: cooltype.dll, Version: 5.10.33.20743, time

  • Playlist Issues

    I have iTunes 11.1  on a MacBook Pro with Retina, with OS X Maverick. Numerous playlists show each song as having been played numerous times. How can I reset so that each song of a particular  playlist will only be played and burned once please? Hope