Rownum Problem

I have a situation where i have to create a table based on another table and data will be inserted in a phase by phase manner.
Suppose I have a table A which holds 10,000 rows. I have to create a table B that will hold the first 2000 rows from A. After some processing next 2000 rows(sequentially) will be inserted till all the 10000 rows are inserted in B.
I used this approach
1) To create table
Create table B
Parallel (degree 4 instances 1)
nologging
select /*+ parallel(4,1) */ * from A where rownum<=2000;
2) Now after some processing i have to insert the next 2000 rows. I planned to use this query
Insert /*+ parallel(4,1) */ into B
(Select /*+ parallel(4,1) */ * from A where rownum<=4000
minus
Select /*+ parallel(4,1) */ * from A where rownum<=2000);
Is my query correct for point no 2. One of my friend confused me by saying that theres no guarantee that next 2000 rows will be inserted using this query. Oracle may randomly choose any 2000 rows from table A and not neccessarily the rows will be in sequential order from table A.
Is this right. I dont think so. I may be wrong in my assumption. But i would like all the gurus to guide me in this. Kindly clear my confusion.
Thanks in advance.

Hi,
Your code is wrong because your underlying assumptions are incorrect. The term "first n rows of the table" has no meaning. A table is a heap, it is disorganized, you can NEVER rely on the database to return the rows in any kind of order unless you specify an ORDER BY.
Your initial query is saying:
"Give me 4000 arbitrary records from the database. Then give me another 2000 arbitrary records. If any records in the first set happen to have been selected in the second set, remove them from the result."
This is how ROWNUM works. It is simply incremented each time a record is returned. It has no connection to an ordering in the table (since there is no concept of "order" in a relational table).
cheers,
Anthony

Similar Messages

  • ROWNUM Problem - Urgent

    Hi,
    I have a wierd problem with using ROWNUM for pagination. I'm using the query structure as below:
    SELECT * FROM ( SELECT A.*, ROWNUM rowno FROM (MY_ACTUAL_QUERY) A WHERE ROWNUM <= 200 ) WHERE rowno > 0
    In some cases of the MY_ACTUAL_QUERY, there exists records, but the pagination query is not returing records. If the where clause is replaced with "ROWNUM <= 361" or "ROWNUM <= 251" in some other cases, it returns records.
    Also, when some fields in the select clause are commented out, the query returns records. Is this a problem with Oracle itself or am I doing something wrong?
    Regards,
    Venkat

    Hi
    in the past.. i have a oracle bug whit pagination querys when i use a ROW_NUMBER ... I reported them at Oracle (bug #7033904)
    Text of bug at Metalink "...If a query contains multiple analytic functions and one of them is rank, dense_rank or row_number function and if there is a topn predicate (i.e. restricting the rows to topn ranks, dense_rank or row_number) in the outer query and if the results for the other analytic functions are incorrect, you may have encountered this bug...."
    This bug is already fixed on 11.2.0.1.0.
    My problem was by the optimizer mode, when the optimizer mode = all_rows the query return "bad results" and when the optimizer mode = rule return "good results"
    try to run the query with the optimizer = rule and let me know what happen ...
    alter session set optimizer_mode=rule; One Metalink note about that.. 734502.1
    Edited by: dask99 on Oct 23, 2008 1:50 PM

  • Combobox values based on query

    I created combobox but how can I set its record source to be based on a query ?

    Well it was no working, however I did it myself like this:
    Declare
    num number;
    my_spid varchar2(9);
    my_desc varchar2(25);
    myvar varchar2(9);
    i number;
    BEGIN
    select count(*) into num from specialty_master;
    i:= 1;
    loop
    myvar:='SP00'||i;
    SELECT description, sp_id INTO my_desc, my_spid FROM specialty_master where sp_id=myvar;
    Add_List_Element('SP_ID', i, my_desc, my_spid);
    i := i + 1;
    exit when i > num;
    end loop;
    END;
    My Sp_id in database was like: SP001,SP002,SP003
    I had to use myvar:='SP00'||i because of some rownum problem.
    But now my error is: No initial value selected for listbox.
    How to set initial value.

  • Oracle 11g - Problem in referring ROWNUM in the SQL

    Hello All,
    We are facing a strange problem with Oracle 11g (11.2.0.1.0).
    When we issue a query which refers the rownum, it returns a invalid record ( which is not exists in the table).
    The same sql is working fine once we analyze the table
    Note: The same sql is working fine with oracle 10g (Before analyze also).
    The script to reproduce the issue:
    DROP TABLE BusinessEntities;
    CREATE TABLE BusinessEntities
    business_entity_id VARCHAR2(25) PRIMARY KEY,
    business_entity_name VARCHAR2(50) NOT NULL ,
    owner_id VARCHAR2(25) ,
    statutory_detail_id NUMBER ,
    address_id NUMBER NOT NULL
    DROP TABLE BusEntityRoles;
    CREATE TABLE BusEntityRoles
    business_entity_id VARCHAR2(25) NOT NULL,
    role_id VARCHAR2(10) NOT NULL,
    PRIMARY KEY (business_entity_id, role_id)
    INSERT
    INTO businessentities ( business_entity_id , business_entity_name, owner_id , statutory_detail_id , address_id)
    VALUES
    ( 'OWNER', 'OWNER Corporation Ltd', NULL , 1, 1 );
    INSERT
    INTO businessentities ( business_entity_id , business_entity_name, owner_id , statutory_detail_id , address_id)
    VALUES
    ( 'ALL_IN_ALL', 'ALL IN ALL Corporation Ltd', 'OWNER' , 2, 2 );
    INSERT INTO busentityroles(business_entity_id, role_id) VALUES ('TEST' , 'OWNER');
    INSERT INTO busentityroles (business_entity_id,role_id) VALUES ('TEST','VENDOR');
    INSERT INTO busentityroles(business_entity_id, role_id) VALUES ('ALL_IN_ALL' , 'VENDOR');
    SELECT *
    FROM
    (SELECT raw_sql_.business_entity_id, raw_sql_.business_entity_name, raw_sql_.owner_id, raw_sql_.address_id,
    rownum raw_rnum_
    FROM
    (SELECT *
    FROM BusinessEntities
    WHERE (business_entity_id IN
    (SELECT business_entity_id
    FROM BusinessEntities
    WHERE business_entity_id = 'OWNER'
    OR owner_id = 'ALL_IN_ALL'
    AND business_entity_id NOT IN
    (SELECT business_entity_id FROM BusEntityRoles
    ORDER BY business_entity_id ASC
    ) raw_sql_
    WHERE rownum <= 5
    WHERE raw_rnum_ > 0;
    OUTPUT Before Analyzing
    BUSINESS_ENTITY_ID: OWNER
    BUSINESS_ENTITY_NAME: NULL
    OWNER_ID: OWNER
    ADDRESS_ID: NULL
    RAW_RNUM_: 1
    Note: There is no record in the table with the value business_entity_id as 'OWNER' and OWNER_ID as 'OWNER' and the address_id as NULL
    OUTPUT : After analyzed the table Using the below mentioned command
    ANALYZE TABLE "BUSENTITYSUPPLYCHAINROLES" ESTIMATE STATISTICS
    ANALYZE TABLE "BUSINESSENTITIES" ESTIMATE STATISTICS
    BUSINESS_ENTITY_ID: OWNER
    BUSINESS_ENTITY_NAME: OWNER Corporation Ltd
    OWNER_ID: NULL
    ADDRESS_ID: 1
    RAW_RNUM_: 1
    Any clue why Oracle 11g is behaving like this.

    Hi,
    it's a good practice to give aliases for tables, as well as name query blocks. Here it is (and formatted for convinience):
    select --/*+ gather_plan_statistics optimizer_features_enable('10.2.0.4') */
      from (select /*+ qb_name(v2) */
                   raw_sql_.business_entity_id
                  ,raw_sql_.business_entity_name
                  ,raw_sql_.owner_id
                  ,raw_sql_.address_id
                  ,rownum raw_rnum_
              from (select /*+ qb_name(v1) */ *
                      from businessentities b1
                     where (b1.business_entity_id in
                           (select /*+ qb_name(in) */ b2.business_entity_id
                               from businessentities b2
                              where business_entity_id = 'OWNER'
                                 or owner_id = 'ALL_IN_ALL'
                                and business_entity_id not in
                                   (select /*+ qb_name(not_in) */ r.business_entity_id from busentityroles r)))
                     order by business_entity_id asc) raw_sql_
             where rownum <= 5)
    where raw_rnum_ > 0;You are facing some bug - definitely - and, possibly, it is caused by [join elimination|http://optimizermagic.blogspot.com/2008/06/why-are-some-of-tables-in-my-query.html]. As a workaround you should rewrite the query to eliminate unnecessary join manually; or you may include a hint to not eliminate join (it's not documented):
    SQL>
    select -- /*+ gather_plan_statistics optimizer_features_enable('10.2.0.4') */
      from (select /*+ qb_name(v2)  */
                   raw_sql_.business_entity_id
                  ,raw_sql_.business_entity_name
                  ,raw_sql_.owner_id
                  ,raw_sql_.address_id
                  ,rownum raw_rnum_
              from (select /*+ qb_name(v1) no_eliminate_join(b1) */ *
                      from businessentities b1
                     where (b1.business_entity_id in
                           (select /*+ qb_name(in) */ b2.business_entity_id
                               from businessentities b2
                              where business_entity_id = 'OWNER'
                                 or owner_id = 'ALL_IN_ALL'
                                and business_entity_id not in
                                   (select /*+ qb_name(not_in) */ r.business_entity_id from busentityroles r)))
                     order by business_entity_id asc) raw_sql_
             where rownum <= 5)
    20   where raw_rnum_ > 0;
    BUSINESS_ENTITY_ID        BUSINESS_ENTITY_NAME                               OWNER_ID                  ADDRESS_ID  RAW_RNUM_
    OWNER                     OWNER Corporation Ltd                                                                 1          1Strange thing is executing a transformed query gives correct result too:
    SELECT "from$_subquery$_001"."BUSINESS_ENTITY_ID" "BUSINESS_ENTITY_ID",
           "from$_subquery$_001"."BUSINESS_ENTITY_NAME" "BUSINESS_ENTITY_NAME",
           "from$_subquery$_001"."OWNER_ID" "OWNER_ID",
           "from$_subquery$_001"."ADDRESS_ID" "ADDRESS_ID",
           "from$_subquery$_001"."RAW_RNUM_" "RAW_RNUM_"
      FROM  (SELECT /*+ QB_NAME ("V2") */
                    "RAW_SQL_"."BUSINESS_ENTITY_ID" "BUSINESS_ENTITY_ID",
                    "RAW_SQL_"."BUSINESS_ENTITY_NAME" "BUSINESS_ENTITY_NAME",
                    "RAW_SQL_"."OWNER_ID" "OWNER_ID","RAW_SQL_"."ADDRESS_ID" "ADDRESS_ID",
                    ROWNUM "RAW_RNUM_"
               FROM  (SELECT /*+ QB_NAME ("V1") */
                            "SYS_ALIAS_1"."BUSINESS_ENTITY_ID" "BUSINESS_ENTITY_ID",
                            "SYS_ALIAS_1"."BUSINESS_ENTITY_NAME" "BUSINESS_ENTITY_NAME",
                            "SYS_ALIAS_1"."OWNER_ID" "OWNER_ID",
                            "SYS_ALIAS_1"."STATUTORY_DETAIL_ID" "STATUTORY_DETAIL_ID",
                            "SYS_ALIAS_1"."ADDRESS_ID" "ADDRESS_ID"
                       FROM "TIM"."BUSINESSENTITIES" "SYS_ALIAS_1"
                      WHERE ("SYS_ALIAS_1"."BUSINESS_ENTITY_ID"='OWNER'
                          OR "SYS_ALIAS_1"."OWNER_ID"='ALL_IN_ALL' AND  NOT
                             EXISTS (SELECT /*+ QB_NAME ("NOT_IN") */ 0
                                       FROM "TIM"."BUSENTITYROLES" "R"
                                      WHERE "R"."BUSINESS_ENTITY_ID"="SYS_ALIAS_1"."BUSINESS_ENTITY_ID")
                      ORDER BY "SYS_ALIAS_1"."BUSINESS_ENTITY_ID") "RAW_SQL_"
             WHERE ROWNUM<=5) "from$_subquery$_001"
    26   WHERE "from$_subquery$_001"."RAW_RNUM_">0
    27  /
    BUSINESS_ENTITY_ID        BUSINESS_ENTITY_NAME                               OWNER_ID                  ADDRESS_ID  RAW_RNUM_
    OWNER                     OWNER Corporation Ltd                                                                 1          1

  • Problem combining select, order by, rownum (top-N) and for update

    Hello,
    i have serious problems with this.
    -- drop table testtable;
    create table testTable (id number(10,0) primary key, usage number(10,10));
    -- delete from testtable;
    insert into testtable values (11, 0.5);
    insert into testtable values (10, 0.3);
    insert into testtable values (12, 0.3);
    insert into testtable values (9, 0.3);
    insert into testtable values (8, 0.9);
    insert into testtable values (3, 0.0);
    insert into testtable values (2, 0.02);
    insert into testtable values (1, 0.05);
    insert into testtable values (7, 0.7);
    insert into testtable values (6, 0.4);
    insert into testtable values (5, 0.2);
    insert into testtable values (4, 0.1);
    select * from testtable;
    -- without FOR UPDATE
    select * from (
    select tt.id id_, tt.*
    from testtable tt
    where tt.usage > 0.1
    order by tt.usage desc, tt.id desc
    where rownum <= 10;
    --> WORKS
    -- without ORDER BY
    select * from (
    select tt.id id_, tt.*
    from testtable tt
    where tt.usage > 0.1
    where rownum <= 10
    for update of id_;
    --> WORKS
    -- without WHERE ROWNUM <= 10
    select * from (
    select tt.id id_, tt.*
    from testtable tt
    where tt.usage > 0.1
    order by tt.usage desc, tt.id desc
    for update of id_;
    --> WORKS
    -- But what i need is this:
    select * from (
    select tt.id id_, tt.*
    from testtable tt
    where tt.usage > 0.1
    order by tt.usage desc, tt.id desc
    where rownum <= 10
    for update;
    --> ORA-02014: cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc., SQL State: 42000, Error Code: 2014
    select * from (
    select tt.id id_, tt.*
    from testtable tt
    where tt.usage > 0.1
    order by tt.usage desc, tt.id desc
    where rownum <= 10
    for update of id_;
    --> ORA-02014: cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc., SQL State: 42000, Error Code: 2014
    I have tried every single solution i could come up with.
    But nothing worked.
    My latest idea is to include a comment in the query and set up an ON SELECT trigger which evaluates the comment and enforeces the lock.
    But i'm not sure if this is even possible.
    I cannot split the statement into two because i need the lock immediately when the wanted rows are selected.
    One major criteria for the rows is the order by. Without it i get a random set of rows.
    And the rownum <= 10 is also needed because i don't want to lock the whole table but only the few needed rows.
    I tried row_number() over (order by ...) but this is considdered a window/group-function which disallows the for update as well as the order by.
    During these tests i noticed, that when using the row_number()-function the resultset is ordered automatically (without an additional order by clause).
    But this doesn't help anyway.
    I tried using piped functions to wrap the select to apply the rownum manually by cursor skip, but this doesn't work either. First of all i wasn't able to wrap the query the way i imagined and second the lock would be applied to the whole resultset anyway but only the reduced rows would be returned.
    I heared about LOCK-hints from other DBs, is there anything similar here?
    Any other solution??
    btw. it has to be high-performance after all.
    Greetings Finomosec;

    No, not perfect.
    This is the expected result (ordered by usage desc, id desc):
    ID     USAGE
    8     0.9
    7     0.7
    11     0.5
    6     0.4
    12     0.3
    10     0.3
    9     0.3
    5     0.2
    This ist the one produced by your statement:
    ID     USAGE
    5     0.2
    6     0.4
    7     0.7
    8     0.9
    9     0.3
    10     0.3
    11     0.5
    12     0.3
    Use limit 5 in your tests, and you will also notice that the following doesn't work either:
    select * from testtable ww where ww.id in
    select id from
    select tt.id, tt.usage
    from testtable tt
    where tt.usage > 0.1
    where rownum <= 5
    order by usage desc, id desc
    for update;
    It's because the order is not applied to the result.
    But the following modification works (at least principally):
    select * from testtable ww where ww.id in
    select id from
    select tt.id, tt.usage
    from testtable tt
    where tt.usage > 0.1
    order by usage desc, id desc
    where rownum <= 5
    order by usage desc, id desc
    for update;
    Thr problem here is:
    I need to expand the following Statement to the above form by (Java-) Code:
    -- statement-A
    select tt.id, tt.usage
    from testtable tt
    where tt.usage > 0.1
    order by usage desc, id desc;
    The main problem is:
    The order by clause needs to be duplicated.
    The problem here is, to identify it (if present) and NOT any order by in inner selects.
    I am using Hibernate and to implement this i have to modify the OracleDialect to solve this problem. I get the statement-A (see above) and have to apply the limit.
    Isn't there any other solution??
    Greetings Finomosec;

  • PROBLEM WITH ROWNUM

    Hello everybody,
    I'm using oracle 8i server. And I'm trying to get two rows out of many, that have largest value for 1st column of the table. I.E Ist and 2nd maximum value.
    The problem is when I'm using
    "select * from table
    where rownum <= 2
    order by 1 desc"
    I'm not getting first 2 rows having largest values in 1st column.
    Instead I get two rows that aren't having the largest value.
    can anybody help me on this error ?
    Or can anybody suggest me a way to do that ?
    is this way the correct one ?

    I appreciate everybody answers, well on step ahead answer to it, If you wanna to use '=' OR a situation where you want to know the second/specific position records then rownum wont work for you
    try this...
    -- for Second Position
    select id from (select id , dense_rank() (Over Partition by ID Order by ID desc) Rank
    from table
    order by id desc) where rank = 2
    Or
    -- In case upto 2 nd position
    select alias.id from (select id , dense_rank() (Over Partition by ID Order by ID desc) Rank
    from table
    order by id desc) where rank <= 2
    -- In case above 2nd position
    select alias.id from (select id , dense_rank() (Over Partition by ID Order by ID desc) Rank
    from table
    order by id desc) where rank >= 2
    Regards,
    Srichan.

  • Order By clause problem with ENAME and Rownum?

    From Scott/Tiger user I run this query from EMP table.
    select rownum,EMPNO,ENAME,JOB from emp
    order by Ename;
    Output is like this
    ROWNUM EMPNO ENAME JOB
    11      11     ADAMS CLERK
    2      2      ALLEN SALESMAN
    6      6      BLAKE MANAGER
    7      7      CLARK MANAGER
    13      13     FORD ANALYST
    12      12     JAMES CLERK
    4      4      JONES MANAGER
    9      9      KING PRESIDENT
    5      5      MARTIN SALESMAN
    14      14      MILLER CLERK
    8      8      SCOTT ANALYST
    1      1      SMITH CLERK
    10      10      TURNER SALESMAN
    3      3      WARD SALESMAN
    Here Rownum order is disturb, I want it also in Ascending order with ENAME.
    Please send the solution of this problem
    Best Regards,
    Shahzad

    select row_number() over (order by ename) as rn, empno, ename, job from emp
      order by ename;

  • Rownum and contains() problem

    hi,I meet a strange problem.I employ rownum and full text index in my sql as follow.But it does not works well ,and nothing can be displayed at all.
    The most strangest is , the sql whitch lies from line 3 to line 11 can works well(It could display what I want),but the whole can't.
    anyone could lend me a hand?THX.
    1 select *
    2 from (
    3 select row_.*,rownum as rn
    4 from (
    5 select *
    6 from node c, s_title s1
    7 where exists(select y.PKID FROM NODE_TYPE y WHERE y.PKID=s1.NODE_ID AND
    8 y.PKID=c.PKID)
    9 and contains(s1.value,'abc',1)>0 ORDER BY score(1) desc ,ORDERNO asc
    10 ) row_
    11 where rownum < 20
    12 )
    13 where rn>0

    But,to my surprise,nothing can be displayed.Why are you surprised?
    SQL is too dumb to lie to you.
    no rows is the correct result set.
    SELECT *
    FROM   (SELECT row_.*,
                   ROWNUM AS rn
            FROM   (SELECT   *
                    FROM     node c,
                             s_title s1
                    WHERE    EXISTS (SELECT y.pkid
                                     FROM   node_type y
                                     WHERE  y.pkid = s1.node_id
                                            AND y.pkid = c.pkid)
                             AND Contains(s1.VALUE,'abc',1) > 0
                    ORDER BY Score(1) DESC,
                             orderno ASC) row_
            WHERE  ROWNUM < 20)
    WHERE  rn > 0

  • Rownum and contains() problem.THX

    hi,I meet a strange problem.
    I employ rownum and full text index in my sql as follow.But it does not works well ,and nothing can be displayed at all.
    The most strangest is , the sql which lies from line 3 to line 11 can works well(It could display what I want),but the whole(1~13) can't.
    anyone could find out the problem?THX.
    1 select *
    2 from (
    3 select row_.*,rownum as rn
    4 from (
    5 select *
    6 from node c, s_title s1
    7 where exists(select y.PKID FROM NODE_TYPE y WHERE y.PKID=s1.NODE_ID AND
    8 y.PKID=c.PKID)
    9 and contains(s1.value,'abc',1)>0 ORDER BY score(1) desc ,ORDERNO asc
    10 ) row_
    11 where rownum < 20
    12 )
    13 where rn>0

    Please either post including a 4 digit Oracle version, or do not post at all.
    Please also don't post vague generalities like 'works well' or 'doesn't work well'
    Either post output, required output, or do not post at all.
    Crystal balls have worn out here long since because many similar posters 'couldn't do the needful'
    Sybrand Bakker
    Senior Oracle DBA

  • Having problem using "rownum" to retrieve only a particular number of rows

    Hi,
    I have a table of some 18 rows filled with certain values and when I do this,
    select * from t where rownum<10; it's working fine. and is giving 9 rows as the result but when I do this,
    select * from t where rownum>1; it's giving me no rows selected as answer. Also, when I do this,
    select * from t where rownum=1; it's working fine but when I do this,
    select * from t where rownum=2; it's not working. This is all strange am I doing wrong somewhere?
    Thanks.

    select * from t where rownum>1; it's giving me no rows selected as answer. We should not use > operator directly with rownum.
    This is because the first row fetched is assigned a rownum 1 and makes the codition false. The second row to be fetched is now the first row and is also assigned a ROWNUM of 1 and makes the condition false. All rows subsequently fail to satisfy the condition, so no rows are returned.

  • Strange problem with rownum

    Hi,
    In the query below, I am using rownum to use as a field to join the output to another query.
    It works fine and give the desired output. But after I made some updates to the undelying table (total_table), the query does not return anything. When I remove the rownum field it works fine but if I use with the rownum it does not return anyting. I tried to run the inner query with the rownum and it runs fine.
    Finally I dropped and recreated the table and the query runs fine. After a day I again had to make some updates to the table after which it seemed to work fine for some time (after the updates). But this morning it behaved similarly. I dropped and recreated again and it is fine now.
    Any insight?
    select rownum r2, ab, total_1,
    from (select substr(file_name, 2,12) ab , total_1 from total_table where month_value = (select to_char(add_months(sysdate, 1), 'MON YYYY') from dual) order by 1,2)
    Thanks,

    rownum are allowed in the outer or main query.
    SQL> select rownum, e.*
      2    from (select empno, ename, job, sal
      3            from emp
      4          order by sal desc) e
        ROWNUM EMPNO ENAME      JOB             SAL
             1  7839 KING       PRESIDENT   5000.00
             2  7902 FORD       ANALYST     3000.00
             3  7788 SCOTT      ANALYST     3000.00
             4  7566 JONES      MANAGER     2975.00
             5  7698 BLAKE      MANAGER     2850.00
             6  7782 CLARK      MANAGER     2450.00
             7  7950 TINA       SALESMAN    1850.00
             8  7945 CINDY      SALESMAN    1800.00
             9  7499 ALLEN      SALESMAN    1600.00
            10  7844 TURNER     SALESMAN    1500.00
            11  7934 MILLER     CLERK       1300.00
            12  7521 WARD       SALESMAN    1250.00
            13  7654 MARTIN     SALESMAN    1250.00
            14  7876 ADAMS      CLERK       1100.00
            15  7900 JAMES      CLERK        950.00
            16  7369 SMITH      CLERK        800.00
    16 rows selected
    SQL> can you try to post some sample update statements that you have used. so we can try to reproduce the issue based on the sample data you have posted.

  • Rownum unexpected problem

    Hello all.
    I'm not sure if this is the right forum but I'll give it a try:
    I have a table which represents salaries per month for employees. Each Employee has a record for each month he got salary. I want to calculate the top 10 employees with the highest earnings. This is the sum of the salary column. At the end I want to return Emp_Id, Sum(Salary) limited to 10 rows. What I tried was: select Emp_id,sum(salary) from emp where runnum <10 group by emp_id.
    This didn't work properly because it only returned the sum of the employees who worked more than 9 months (rownum <10).
    This is not good for me. How can I achieve what I need?
    Thank you
    Liron
    null

    Hi,
    If u'r using Oracle 8.1 and above this works.
    Here is u'r query
    Select A.Empno, A.Salary
    From (Select A.Emp Empno, A.Sal Salary
    From (Select Empid Emp, Sum(Sal) Sal
    From EMp
    group by empid) A
    order by a.sal Desc
    Where Rownum < 10
    Regards
    Ganesh R
    null

  • Problem with selecting Rownum

    Hello,
      1* select articles_id,subject from Articles where active=1 and cat_id=2 order by articles_id desc
    SQL> /
    ARTICLES_ID SUBJECT
              7 Article-7
              6 Article-6
              5 Article-5
              4 Article-4
              3 Article-3
              2 Article-2
              1 Article-1
    7 rows selected.
      1* select articles_id,subject from Articles where active=1 and cat_id=2 and rownum<2 order by arti
    SQL> /
    ARTICLES_ID SUBJECT
              2 Article-2Why rownum<2 is not working?
    Best regards

    rownum is a psuedo column assigned to a row. It starts at 1 and ONLY increments to 2 and
    above if you actually output a row.
    Consider the query:
    Select * from t where rownum = 2
    The psuedo code for that would be:
    Rownum = 1
    For x in ( select * from t )
    Loop
    if ( rownum = 2 )
    then
    output record
    rownum = rownum+1;
    end if
    End loop
    therefore, you never get to row 2, so this produces ZERO rows.
    ref: http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:44376372354520

  • Problems with where rownum = 2, but not rownum = 1.

    Can anyone explain to me why I get the expected result when I do
    rownum = 1
    rownum <= 2
    rownum < 3
    but not for
    rownum > 1
    rownum = 2
    SQL> create table test(a number, b number);
    Table created.
    SQL> insert into test VALUES(1,2);
    1 row created.
    SQL> insert into test VALUES(2,3);
    1 row created.
    SQL> select rownum, a, b from (select a,b from test where a < 10 order by b desc);
    ROWNUM A B
    1 2 3
    2 1 2
    SQL> select rownum, a, b from (select a,b from test where a < 10 order by b desc) where rownum = 1;
    ROWNUM A B
    1 2 3
    SQL> select rownum, a, b from (select a,b from test where a < 10 order by b desc) where rownum > 1;
    no rows selected
    SQL> select rownum, a, b from (select a,b from test where a < 10 order by b desc) where rownum = 2;
    no rows selected
    SQL> select rownum, a, b from (select a,b from test where a < 10 order by b desc) where rownum <= 2;
    ROWNUM A B
    1 2 3
    2 1 2
    SQL> select rownum, a, b from (select a,b from test where a < 10 order by b desc) where rownum < 3;
    ROWNUM A B
    1 2 3
    2 1 2
    SQL>

    ROWNUM is only incremented when a record passes the WHERE clause (but ROWNUM is assigned before the rows are ordered).
    http://download-uk.oracle.com/docs/cd/B19306_01/server.102/b14200/pseudocolumns009.htm#i1006297
    So if you need to use a lower bound for ROWNUM, you must use a subquery. And as you usually need first/second/third record when sorting by field x, there will be two levels of subqueries.
    SELECT *
    FROM (SELECT *, rownum rn
    FROM (SELECT ...your query... ORDER BY x)
    WHERE rownum<=2
    WHERE rn=2

  • Problems copying a row and pasting it in the following row

    Hi all,
    In a Numbers table I have 400 rows with data separated each one by a blank row
    row 2 has data
    row 3 is blank
    row 4 has data
    row 5 is blank ... and so on.
    I have prepared a small script to copy row 2 into row 3, row 4 into row 5,... as follows:
    set dName to "Conta"
    set sName to "Movis"
    set tName to "Pruebas"
    set row_ini to 2
    tell application "Numbers" to tell document dName to tell sheet sName to tell table tName
    set selection range to row row_ini
    repeat
    if value of first cell of selection range = 0 then
    exit repeat
    else
    tell application "System Events"
    keystroke "c" using {command down}
    keystroke return
    keystroke "v" using {command down}
    end tell
    tell first cell of the selection range to set rowNum to address of its row
    set selection range to row (rowNum + 1)
    end if
    end repeat
    end tell
    The idea is:
    1 select the initial row (in this case row 2)
    2 check if the value of first cell of the row is cero, then exit the repeat loop
    3 else
    4 copy the entire row, move down a row and paste.
    5 move to the next row
    6 repeat
    The first time I run the script it worked until the row 124 and stopped. I tried again changing the inicial row and it worked only for 7 rows. The third time it erased all the cells with data. I've tried closing both the table and the script but the problems persists: in this case it stops after replacing 5 rows.
    Where am I wrong?
    Thank you in advance
    Ratz

    KOENIG Yvan wrote:
    There is not such a book.
    There is just on short chapter dedicated to iWork in :
    Hanaan Rosenthal & Hamish Sanderson, Learn AppleScript: The Comprehensive
    Guide to Scripting and Automation on Mac OS X, Third Edition, Apress (2010);
    ISBN 978-1-4302-2361-0
    You may find useful infos in my idisk :
    <http://public.me.com/koenigyvan>
    Scan the contents of the folder :
    For_iWork:iWork '09:for_Numbers09.
    I guess the problem is for using the tell application block without telling the process Numbers.
    No, I had to change a lot of things.
    the name of the document was wrong (at least on my machine on which I always use the name extension.
    set selection range … was wrong.
    Yvan KOENIG (VALLAURIS, France) dimanche 20 février 2011 23:02:35
    Thank you.
    Visiting your idisk I realise how much work I must do to become a decent scripter!
    The book you mention looks interesting. I'm going to include it in my bookcase (and read it, of course).
    Ratz

Maybe you are looking for

  • Can My New Hard Drive Be Causing Back-Up Weirdness?

    Last week, I bought a new Seagate 100GB/7200 rpm HD, which I've been using externally in a Firewire enclosure. I wanted to try it out for a week before I install it into my Pismo. I've been using computers for 20 years, and I'm conscientious about sa

  • SRM- Pucchase Order and Contract (backend) Tolerance failures

    Using SRM 7 Extended classic scenario with ECC 6.0.  Tolerances have been set up in ECC and SRM.  The Contract is raised in the backend and replicated into SRM.  In SRM, both PPOMA and Vendor groups have been assigned the correct TOG. Raising a Shopp

  • Workspace displays all timeline objects at once

    As I recall Captivate 1, the workspace was like Flash: It only displayed the objects that appear on screen where the playhead (red line) is located in the timeline. Since I upgraded to v5, the workspace always displays EVERYTHING. If I move the playh

  • How do I name a photo album in Iphoto?

    Hello!  I am a brand new user and need a little help.  I have an iphone an ipad mini and a mac pro book. Lots of toys but limited knowledge.  I am trying to work in Iphoto.  Experimenting is fun and I have been able to play and do somethings includin

  • Highlight single data bar in chart

    Hi - I'm wondering if anyone has figured a non-complicated way to to change the color on only ONE bar of a single data set in a bar chart. Thanks!