Rownum, Order by, inner-inner query

I have a query where the current approach to handling rownum and order by's doesn't work and was wondering if anyone knew of a way around this. The basic query is as follows:
Select *
From <list of tables> T1
Where....
And t1.elig_id =
(Select elig_id
From (Select elig_id
From eligibility T2
where T1.family_number = T2.family_number
order by date1 desc, date2 desc)
Where rownum = 1)
The problem is the T1.family_number = T2.family_number line. Since the join is in the inner-inner query sql does not recognize T1.family_number. Is there anyway around that?

It is probably not materializing the sub query for every row in the outer select, but IN can be expensive if the sub-query returns a large number of rows. Another way to phrase the statement which may be faster is:
SELECT t1.*
FROM <list of tables> T1,
     (SELECT elig_id
      FROM (SELECT elig_id, ROW_NUMBER() OVER (PARTITION BY family_number
                                           ORDER BY date1 DESC date2 DESC) rn
            FROM eligibility)
      WHERE rn = 1) elig
Where t1.elig_id = elig.elig_id and
      other_conditionsEssentially, use the sub-query as if it were a table (an in-line view actually), and join to it. This may make Oracle materialize it earlier and avoid the inlist processing.
HTH
John

Similar Messages

  • I can´t select rownum in a query, when inner query contains SDO_ANYINTERACT

    I have written a query in Oracle that looks like this:
    select ID, NAME, GEOMETRY from
    select a.*, rownum as rnm from
    select ID, NAME, GEOMETRY from MY_TABLE
    where SDO_ANYINTERACT(GEOMETRY, SDO_UTIL.SDO_GEOMETRY('POLYGON ((670000 6268000, 670000 6269000, 700000 6269000, 700000 6268000, 670000 6268000))')) = 'TRUE'
    order by NAME asc
    ) a
    where rnm <= 50 and rnm >= 40
    The inner query is selecting rows from MY_TABLE using a bounding box. The outer queries are included to enable paging for the results.
    For some odd reason this query does not yield any results. If I try and run the subquery:
    select ID, NAME, GEOMETRY from MY_TABLE
    where SDO_ANYINTERACT(GEOMETRY, SDO_UTIL.SDO_GEOMETRY('POLYGON ((670000 6268000, 670000 6269000, 700000 6269000, 700000 6268000, 670000 6268000))')) = 'TRUE'
    order by NAME asc
    It yields a list of results as expected. If i run the subquery:
    select a.*, rownum as rnm from
    select ID, NAME, GEOMETRY from MY_TABLE
    where SDO_ANYINTERACT(GEOMETRY, SDO_UTIL.SDO_GEOMETRY('POLYGON ((670000 6268000, 670000 6269000, 700000 6269000, 700000 6268000, 670000 6268000))')) = 'TRUE'
    order by NAME asc
    ) a
    the result set is empty. Somehow rownum is preventing the query from yielding any results. If I remove rownum the results are returned as in the innermost query:
    select a.* from
    select ID, NAME, GEOMETRY from MY_TABLE
    where SDO_ANYINTERACT(GEOMETRY, SDO_UTIL.SDO_GEOMETRY('POLYGON ((670000 6268000, 670000 6269000, 700000 6269000, 700000 6268000, 670000 6268000))')) = 'TRUE'
    order by NAME asc
    ) a
    What am I doing wrong here?? I am running Oracle 10g..
    Edited by: user12456076 on 2010-01-15 06:14

    What version of Oracle are you running?
    I tried your query on 10.2.0.2 on some data I have and your query works properly.
    Though I would shorten the query to:
    select ID, NAME, GEOMETRY from
      select rownum as rnm, ID, NAME, GEOMETRY
        from MY_TABLE
       where SDO_ANYINTERACT(GEOMETRY, SDO_UTIL.SDO_GEOMETRY('POLYGON ((470000 5268000, 470000 5269000, 500000 5269000, 500000 5268000, 470000 5268000))')) = 'TRUE'
       order by NAME asc
    ) a
    where rnm <= 50 and rnm >= 40;regards
    Simon

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

  • IS IT POSSIBLE TO  WRITE ORDER BY CLAUSE WITHIN INNER QUERY

    IS IT POSSIBLE TO WRITE ORDER BY CLAUSE WITHIN INNER QUERY

    So you still can't :) I still don't see it that strict:
    You know of course that this is possible:
    select ename, (select ename
                     from (select   empno, ename
                               from emp
                              where deptno = 10
                           order by 1) e2
                    where e.empno = e2.empno) a
      from emp eso we have an »ORDER BY CLAUSE WITHIN INNER QUERY« which is even correlated (though through the outer query).
    Whether this makes sense or not is not question imho :-) ... but you can

  • Error when trying to add a hint to inner query in WHERE clause

    I have a form where the WHERE clause in a block starts like:
    where seqno in (select seqno from nddf n, restriction r...
    I need to add a hint to the inner query like following:
    where seqno in (select /*+ ORDERED */ seqno from nddf n, restriction r...
    When the query executes. I gets an error message and when I look at the detail error I see: ORA-01722: invalid number
    Does anybody know the cause for this. Can I not use hints in inner queries?
    Thanks,
    Thomas

    Did you mean something like this,
    i have tried the below code in forms 10g , as the where clause of 'EMP' block there was no problem.
    deptno in (select /*+ ORDERED */ deptno from dept where rownum<2);
    Also tried in SQLPlus like below
    SQL> select * from emp where deptno in (select /*+ ORDERED */ deptno from dept where rownum<2);
    EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
    7782 CLARK MANAGER 7839 09-JUN-81 2450 10
    7839 KING PRESIDENT 17-NOV-81 5000 10
    7934 MILLER CLERK 7782 23-JAN-82 1300 10

  • Using the inner query to filter outer query

    hi
    i've tried finding an old post on this, but there's nothing quite what im looking for. I have an inner query which gets sales totals, and an outer query with an outer join to return all the weeks (even when there are no sales):-
    select wk.week_id,sum(sales_total)
    from (select week, sum(sales) sales_total from tbl_sales_A
    where week between 0801 and 0807
    group by week
    UNION ALL
    select week, sum(sales) sales_total from tbl_sales_B
    where week between 0801 and 0807
    group by week)
    , tbl_weeks wk
    WHERE
    wk.week_id=week(+)
    AND wk.week_id BETWEEN 0801 AND 0807
    GROUP BY wk.week_id
    this gives me the following results:-
    week_id sales
    0801 0
    0802 0
    0803 55
    0804 66
    0805 96
    0806 0
    0807 97
    I would like my query to only return results from the first week sales are made i.e. 0803. The results would like this:-
    0803 55
    0804 66
    0805 96
    0806 0
    0807 97
    How do i write my query so that it does this???
    Many thanks for your time.
    Message was edited by:
    user645692

    On a test data:
    SQL> with test_data as (
      2    select 0801 as week_id, 0 as sales_total from dual union all
      3    select 0802, 0  from dual union all
      4    select 0803, 55  from dual union all
      5    select 0804, 60 from dual union all
      6    select 0804, 6  from dual union all
      7    select 0805, 90  from dual union all
      8    select 0805, 6  from dual union all
      9    select 0806, 0 from dual union all
    10    select 0807, 97 from dual
    11  )
    12  select week_id, sales_week_total
    13  from (
    14    select wk.week_id,sum(sales_total) as sales_week_total,
    15      sum(sum(sales_total)) over (order by wk.week_id rows between unbounded preceding and current row) as sales_so_far
    16    from test_data wk
    17    GROUP BY wk.week_id
    18  )
    19  where sales_so_far <> 0
    20  ;
       WEEK_ID SALES_WEEK_TOTAL
           803               55
           804               66
           805               96
           806                0
           807               97Regards,
    Dima
    Message was edited by:
    DimaCit

  • How to create a DB Adapter with select query having inner query

    Hi All,
    I am trying to create a DB Adapter with select query. The query has some inner queries in it. It is just like this select a, b, c, (select d from e) d, (select e from e) e from tablename.
    The problem here is with the xsd generated for this query. Xsd is not getting generated properly for all the fields it is just getting generated till c element and when it encounters
    the inner query it is stopping the generation of xsd. So for the above query the xsd is something similar to the below
    <xs:complexType name="rewOutput">
    <xs:sequence>
    <xs:element name="a" type="xs:string" nillable="true"/>
    <xs:element name="b" type="xs:string" nillable="true"/>
    <xs:element name="c" type="xs:string" nillable="true"/>
    <xs:element name="select_d" type="xs:string" nillable="true"/>
    </xs:sequence>
    </xs:complexType>
    as shown above the xsd is just getting generated till the first inner query. What should be done to get the full fledged xsd. Should it be manually built ?? Please help me on this.
    Thanks In Advance.
    Edited by: 959766 on Nov 30, 2012 1:20 AM

    Hi,
    I don't think the parser will be able to understand your query properly... I would try building the xsd manually...
    Cheers,
    Vlad

  • How to pass parameter in Discoverer inner query  PLZ give solution-URGENT

    Hi All,
    My need is I have a query in Disco Admin which has inner query as mention below:
    select Net_Drawn_Amount,Undrawn_Amount,Business_Date from
    (select * from t_FacilityExtract) A,
    We are creating a workbook using Diso Plus and creating a parameter on Business_date which is date partitioned, but issue is in inner query(select * from t_FacilityExtract) is picking whole data which has millions of rows.
    Plz help me how to filter inner query.
    It is just a sample query.
    Plz help me as soon as possible.
    Thanks in advance
    Amit

    Hi,
    Generally, using database contexts is the best way to pass parameters to folders defined in Disco Admin. You can look at this posts or download a article on using contexts with Discoverer from my website (www.cabotconsulting.co.uk).
    Re: Implementing HR report in Discoverer
    Re: Passing multiple parameters into Custom Folder...
    Re: Parameters in SubQuery
    Re: Passing Parameters to a discoverer folder using Note 282249.1
    Rod West

  • Grouping Inner Query based on a column.Please help

    I have a strange query.
    I am using Subquery to display the count of rows from the inner query
    depending upon the value of 'Y' or 'N'
    Trade
    id_entity       id_inst_code_type   id_inst_code   dt_trade
    AGL            SE                  5660249        10-Feb-06
    AGL            SE                  5660249        13-Feb-06
    AGL            SE                  5660249        13-Feb'06
    Instrument_xref
    ID_inst      id_inst_xref_type     id_inst_xref  flg_active
    0029010             SE          5660249          Y
    0070789          SE          5660249          Y
    0071190          SE          5660249          Y
    0072385          SE          5660249          Y
    0073215          SE          5660249          Y
    0084797          SE          5660249          Y
    0091375          SE          5660249          Y
    0094690          SE          5660249          Y
    0104438          SE          5660249          Y
    My output:
    id_inst_code_type          id_inst_code   Earliest    Latest       Total    Active
    SE                         5660249       10 Feb 06   13 Feb 06    3        9
    2) If all the 'flg_active' column in Table Instrument_xref is set to 'N'
       the Active should be 0.
    3) Assume that the flg_active could be 3 Y's and 6 N's then what?
    id_inst_code_type          id_inst_code   Earliest    Latest       Total    Active
    SE                         5660249       10 Feb 06   13 Feb 06    3        0
    How do I check for the 'Y' or 'N' value in my code below ?
    Help appreciated as the the functionality changes by the hour...
    select    tie.id_entity             'Entity',
              tie.id_inst_code          'Inst Code',
              min(tie.dt_trade)         'Earliest',
              max(tie.dt_trade)         'Latest',
              count(*)                  'Total',
              dt.InnerTotal             'Active'   
    from     trade_input_event tie,
    (Select  insx.id_inst_xref_type,
                   insx.id_inst_xref,
                   insx.flg_active,
                   count(*) InnerTotal
      from instrument_xref insx
      where insx.id_inst_xref = '5660249'
      ---** Do I need to Check the flg_active here..
      ---** Do I need to use the Having clause here? ie having count(insx.id_inst_xref) = 'N'
       group by insx.id_inst_xref_type,insx.id_inst_xref,insx.flg_active) dt
      where tie.id_inst_code = dt.id_inst_xref
      and tie.id_entity = 'AGL'
      group by tie.id_entity, tie.id_inst_code_type,tie.id_inst_code

    As the flg_active is set to 'Y', I am trying to set it to 'N' in the query
    so that count of 0 is returned, but this displays nothing.
    Please help as to how to display a count(*) of 0 when the flg_active is 'N'??
    Select  insx.id_inst_xref_type,
                   insx.id_inst_xref,
                   insx.flg_active,
                   count(*) InnerTotal
      from instrument_xref insx
      where insx.id_inst_xref = '5660249'
      and insx.flg_active = 'N'
      group by insx.id_inst_xref_type,insx.id_inst_xref,insx.flg_active) dt

  • Strange behavior in inner query with group by clause

    Hi All,
    I found a very strange behaviour with Inner query having a group by clause.
    Please look the sample code
    Select b,c,qty from (select a,b,c,sum(d) qty from tab_xyz group by b,c);
    This query gives output by summing b,c wise. But when i run the inner query it gives error as "not a group by expression "
    My question is - though the inner query is wrong, how is it possible that this query gives output.
    it behaves like -
    Select b,c,qty from (select b,c,sum(d) qty from tab_xyz group by b,c);
    Is it a normal behaviour ?
    If it is a normal behaviour, then how group by behaves inside a inner query.
    Thanks !!

    This case I have tested already and it throws error.
    But why the initial posted query is not throwing
    error even the inner query is wrong.
    In what way oracle behaves for the initial posted
    query?what is the scenario that throws the error? is it at the time when you only run the inner query or the whole query?

  • ROWNUM & ORDER BY

    Dear Oracle experts,
    I am not getting the expected output when i use ROWNUM & ORDER BY clause in the same query.
    Here is issue. Let us say, we have a table TEST with field last_update_date.
    CREATE TABLE TEST(LAST_UPDATE_DATE DATE);
    INSERT INTO TEST VALUES(TO_DATE('23-MAY-05'));
    INSERT INTO TEST VALUES(TO_DATE('23-MAY-05'));
    INSERT INTO TEST VALUES(TO_DATE('23-MAY-05'));
    INSERT INTO TEST VALUES(TO_DATE('23-MAY-05'));
    INSERT INTO TEST VALUES(TO_DATE('23-MAY-05'));
    INSERT INTO TEST VALUES(TO_DATE('23-MAY-05'));
    INSERT INTO TEST VALUES(TO_DATE('22-MAY-05'));
    INSERT INTO TEST VALUES(TO_DATE('21-MAY-05'));
    INSERT INTO TEST VALUES(TO_DATE('20-MAY-05'));
    INSERT INTO TEST VALUES(TO_DATE('13-JUN-04'));
    INSERT INTO TEST VALUES(TO_DATE('30-APR-05'));
    INSERT INTO TEST VALUES(TO_DATE('09-MAR-05'));
    INSERT INTO TEST VALUES(TO_DATE('12-MAY-05'));
    select * from TEST
    where trunc(LAST_UPDATE_DATE) >= to_date('09-MAR-05')
    and trunc(LAST_UPDATE_DATE) <= to_date('22-MAY-05')
    and rownum < 3
    order by LAST_UPDATE_DATE;
    The above query is supposed to return
    09-MAR-05
    30-APR-05
    But it is returning
    21-MAY-05
    22-MAY-05
    If i remove ROWNUM, then it returns in the correct order. But i want to restrict number of records in the same query.
    Anybody can help me on this?.
    Regards
    Govind

    Hello all,
    I need yours expertise again.
    I created FBI on last_update_date. It works fine.
    When i run this query without order by clause, it takes only fraction of second to complete the process. If i add order by caluse, it takes for ever to complete the query. The table has 30 million records.
    Anybody can help me on this?.
    SELECT *
    FROM (SELECT *
    FROM TEST
    where trunc(LAST_UPDATE_DATE) >= to_date('09-MAR-05')
    and trunc(LAST_UPDATE_DATE) <= to_date('22-MAY-05')
    order by LAST_UPDATE_DATE)
    where rownum < 3
    Regards
    Govind

  • Parameter for ORDER BY in DAX query not respected

    I have an SSRS table fed by a parameterized DAX query (utilizing the methods in
    thesearticles). I have all of my parameters working just fine except for those feeding my closing ORDER BY statement (I am offloading the sorting
    to the Tabular for several reasons, including a large tested performance gain over sorting in SSRS). I have defined the ORDER BY as follows:
    ORDER BY @Order1, @Order2, @Order3, @Order4
    I cannot get even the first parameter to work. I can pass arbitrary strings with no impact on the report returned, though if I hard code the ORDER BY in the query, the ordering is respected, so I know this is a problem with the parameter.
    Running a Profiler trace on the server when I fire the SSRS report returns the following as the parameter value for @Order1:
    <Parameter>
    <Name>Order1</Name>
    <Value xsi:type="xsd:string">Dimuser[UserID-Name]</Value>
    This is exactly what I want to replace @Order1, and when I hard code that exact string into my query I get the behavior I want.
    I have played with \ escaping the brackets to no avail.
    Any insight is greatly appreciated.

    I have discovered a workable solution to my own problem.
    The parameter is passed and interpreted as a quoted string, rather than as a field name.
    I altered my query to follow this general format:
    ORDER BY
    SWITCH( TRUE()
    , @Order1 = "User", DimUser[UserID-Name]
    , SWITCH( TRUE()
    , @Order2 = "User", DimUser[UserID-Name]
    In this way I am comparing two quoted strings for equality and providing an unquoted field identifier that Tabular recognizes and can order by.

  • Group by order by in same query

    Hi,
    I am wondering whether it is possible to use group by and order by in same query
    For example if i have a table like the one below
    Col1 Col2 col3
    C 36 2
    A 25 5
    B 12 8
    A 25 6
    B 12 9
    C 36 1
    A 25 7
    I need a result like below
    A 25 5
    A 25 6
    A 25 7
    B 12 8
    B 12 9
    C 36 1
    C 36 2
    can the select statement with group by and order by solve this? How?

    Yes you can - though I am not sure that it is what you want. In your example you do not need to group the data, you just need to order by the first column, then the third. If required, you could also throw in the second column;
    SQL> with t as  ( 
       select 'C' col1, 36 col2, 2 col3 from dual union all 
       select 'A', 25 ,5 from dual union all 
       select 'B', 12 ,8 from dual union all 
       select 'A', 25 ,6 from dual union all 
       select 'B', 12 ,9 from dual union all 
       select 'C', 36 ,1 from dual union all   
       select 'A', 25 ,7 from dual)  
    select col1,col2,col3  
    from t
    order by col1,col3
    COL1       COL2       COL3
    A            25          5
    A            25          6
    A            25          7
    B            12          8
    B            12          9
    C            36          1
    C            36          2

  • TIPS(46) : ROWNUM(ORDERING 순으로 NUMBERING, RANGE SELECT)

    제품 : SQL*PLUS
    작성날짜 : 2003-07-30
    TIPS(46) : ROWNUM(ORDERING 순으로 NUMBERING, RANGE SELECT)
    =========================================================
    PURPOSE
    Explanation
    SQL*PLUS에서 ORDERING 순으로 NUMBERING하기를 원하는 경우가 많으나,
    ORDERING이 되기 전에 RANDOM ACCESS 시
    ROWNUM이 ASSIGN되기 때문에, 다음과 같은 결과가 나타날 것이다.
    SQL> select ename, rownum from emp;
    ENAME ROWNUM
    ALLEN 1
    JONES 2
    BLAKE 3
    CLARK 4
    KING 5
    ADAMS 6
    JAMES 7
    FORD 8
    SQL> select ename, rownum from emp order by ename;
    ENAME ROWNUM
    ADAMS 6
    ALLEN 1
    BLAKE 3
    CLARK 4
    FORD 8
    JAMES 7
    JONES 2
    KING 5
    Example
    다음의 몇 가지 방법을 이용하여 ORDERING 순으로 NUMBERING을 나타내어 보자.
    1) RECORD를 COUNT하는 방법 (DATA가 많은 경우 부적절)
    SQL> select A.ename, count(*) position
    2 from emp A, emp B
    3 where A.ename > B.ename
    4 or A.ename = B.ename and A.empno >= B.empno
    5 group by A.empno, A.ename
    6 order by A.ename, A.empno;
    ENAME POSITION
    ADAMS 1
    ALLEN 2
    BLAKE 3
    CLARK 4
    FORD 5
    JAMES 6
    JONES 7
    KING 8
    2) INDEX를 이용하는 방법
    SQL> create index sort_ix on emp (ename);
    Index created.
    SQL> select ename, rownum from emp where ename > ' ';
    ENAME ROWNUM
    ADAMS 1
    ALLEN 2
    BLAKE 3
    CLARK 4
    FORD 5
    JAMES 6
    JONES 7
    KING 8
    cf) descending인 경우 아래처럼 hint 사용 바람
    select /*+ index_desc(emp ename_ix) */
    ename, rownum from emp
    where ename > ' ' and rownum < 4;
    3) OPTIMIZER를 이용하는 방법
    SQL> select rownum, ename
    2 from emp , dual
    3 where emp.ename = dual.dummy (+);
    ROWNUM ENAME
    1 ADAMS
    2 ALLEN
    3 BLAKE
    4 CLARK
    5 FORD
    6 JAMES
    7 JONES
    8 KING
    위에서 언급한 내용에 더해서 rownum을 where 절에 사용함에 있어서는
    rownum 1을 포함하는 range(예 : 'where rownum <= 100')에 대해서만
    정상적인 조회가 가능하다.
    만약 rownum 1을 포함하지 않는 range(예 : 'where rownum between 50 and 100')
    에 대한 자료 조회를 원한다면 다음과 같이 in-line view를 이용하는 방법을
    사용할 수 있다.
    select rn, ename
    from ( select rownum rn, ename
    from emp)
    where rn between 3 and 5 ;
    RN ENAME
    3 BLAKE
    4 CLARK
    5 KING
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE
    1 0 VIEW
    2 1 COUNT
    3 2 TABLE ACCESS (FULL) OF 'EMP'
    * 이 때 in-line view의 rownum에는 반드시 alias를 이용해야 한다.
    in-line view에 의해서 선택되어지는 data가 많다면 performance가
    떨어질 수 있다.
    Reference Documents
    -------------------

    제품 : SQL*PLUS
    작성날짜 : 2003-07-30
    TIPS(46) : ROWNUM(ORDERING 순으로 NUMBERING, RANGE SELECT)
    =========================================================
    PURPOSE
    Explanation
    SQL*PLUS에서 ORDERING 순으로 NUMBERING하기를 원하는 경우가 많으나,
    ORDERING이 되기 전에 RANDOM ACCESS 시
    ROWNUM이 ASSIGN되기 때문에, 다음과 같은 결과가 나타날 것이다.
    SQL> select ename, rownum from emp;
    ENAME ROWNUM
    ALLEN 1
    JONES 2
    BLAKE 3
    CLARK 4
    KING 5
    ADAMS 6
    JAMES 7
    FORD 8
    SQL> select ename, rownum from emp order by ename;
    ENAME ROWNUM
    ADAMS 6
    ALLEN 1
    BLAKE 3
    CLARK 4
    FORD 8
    JAMES 7
    JONES 2
    KING 5
    Example
    다음의 몇 가지 방법을 이용하여 ORDERING 순으로 NUMBERING을 나타내어 보자.
    1) RECORD를 COUNT하는 방법 (DATA가 많은 경우 부적절)
    SQL> select A.ename, count(*) position
    2 from emp A, emp B
    3 where A.ename > B.ename
    4 or A.ename = B.ename and A.empno >= B.empno
    5 group by A.empno, A.ename
    6 order by A.ename, A.empno;
    ENAME POSITION
    ADAMS 1
    ALLEN 2
    BLAKE 3
    CLARK 4
    FORD 5
    JAMES 6
    JONES 7
    KING 8
    2) INDEX를 이용하는 방법
    SQL> create index sort_ix on emp (ename);
    Index created.
    SQL> select ename, rownum from emp where ename > ' ';
    ENAME ROWNUM
    ADAMS 1
    ALLEN 2
    BLAKE 3
    CLARK 4
    FORD 5
    JAMES 6
    JONES 7
    KING 8
    cf) descending인 경우 아래처럼 hint 사용 바람
    select /*+ index_desc(emp ename_ix) */
    ename, rownum from emp
    where ename > ' ' and rownum < 4;
    3) OPTIMIZER를 이용하는 방법
    SQL> select rownum, ename
    2 from emp , dual
    3 where emp.ename = dual.dummy (+);
    ROWNUM ENAME
    1 ADAMS
    2 ALLEN
    3 BLAKE
    4 CLARK
    5 FORD
    6 JAMES
    7 JONES
    8 KING
    위에서 언급한 내용에 더해서 rownum을 where 절에 사용함에 있어서는
    rownum 1을 포함하는 range(예 : 'where rownum <= 100')에 대해서만
    정상적인 조회가 가능하다.
    만약 rownum 1을 포함하지 않는 range(예 : 'where rownum between 50 and 100')
    에 대한 자료 조회를 원한다면 다음과 같이 in-line view를 이용하는 방법을
    사용할 수 있다.
    select rn, ename
    from ( select rownum rn, ename
    from emp)
    where rn between 3 and 5 ;
    RN ENAME
    3 BLAKE
    4 CLARK
    5 KING
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE
    1 0 VIEW
    2 1 COUNT
    3 2 TABLE ACCESS (FULL) OF 'EMP'
    * 이 때 in-line view의 rownum에는 반드시 alias를 이용해야 한다.
    in-line view에 의해서 선택되어지는 data가 많다면 performance가
    떨어질 수 있다.
    Reference Documents
    -------------------

  • How to overwrite an inner query in the select clause

    Hello,
    I have a queryof this form:
    SELECT t1.ID,
               t1.column2,
               (SELECT SUM (t2.column2)
                    FROM table2 t2
                  WHERE t2 = t.ID
                       AND t2.column1 IN (SELECT ..... FROM table3 t3 WHERE t3.column1 = t1.column3 ........)
        FROM table1 t1At the stage where I select from table3 I do not see the columns of table1. In other words:
      t3.column1 = t1.column3will NOT work.
    The version of the database is 10.2.0.4.
    How could I rewrite this piece of code in order to surround the problem?
    I cannot join table3 with table1 in the outermost query, because the structure of this database is such that it would not work for me.
    I know that most of you would say, change the DB structure, but anyway, any ideas? :)
    Besides, is the same restriction for inner queries applicable in Oracle 11g2?
    Thanks

    user13080027 wrote:
    Hi
    I mean I receive the error ORA-00904-"t1.column3" invalid identifier.Something does not add up here. You said earlier
    I cannot join table3 with table1 in the outermost query, because the structure of this database is such that it would not work for me.Why can't you rewrite your query something like follows:
    SELECT t1.ID,
               t1.column2,
               (SELECT SUM (t2.column2)
                    FROM table2 t2
                  WHERE t2 = t.ID
                       AND t2.column1 = t3.column3
        FROM table1 t1, table3 t3
    WHERE t3.column1 = t1.column3p.s. I am not saying this is the exact query you need but then you don't provide any details requested. ;)

Maybe you are looking for