Rownum
I want to know the reason behind the output of both the queries...
1. select * from emp where rownum > 5 ;
Gives output as:
no rows selected
2. select * from emp where rownum < 5 ;
Gives 4 rows from the table .
can anyone help me in that ...
Thanks a lot.............
I want to know the reason behind the output of both
the queries...
1. select * from emp where rownum > 5 ;
Gives output as:
no rows selected
. select * from emp where rownum < 5 ;
Gives 4 rows from the table .In short,
rownum is assigned to the result set of a query.
By saying you want rows where rownum > 5 Oracle comes across the first row of data, says "ok, this row is potentially rownum 1, that doesn't meet the rownum > 5 condition so discard it, let's take the next row.... this next row is now potentially rownum 1, that too doesn't meet the rownum > 5 condition, so discard it, let's take the next row...." and so on in the same vein.
As you can see, Oracle never get's a row to assign a rownum of 1 so it will never get to anything larger than that and all rows will be discarded.
;)
Similar Messages
-
Hi ,
Please help me on this…
Select * from EMP where Emp_Type in (1, 2 ) order by Emp_Type;
the above query gives me the result like this
Emp_Id Name Emp_Type
100 asas 1
101 dsds 1
122 gfgf 1
154 ytytyt 1
125 uyuy 1
153 reree 2
154 ytytt 2
600 trtrtr 2
700 gfghf 2
If I pass the Start with num 2 and give 3 as number of records to be displayed. I need to retrieve only the following rows..
Emp_Id Name Emp_Type
101 dsds 1
122 gfgf 1
154 ytytyt 1
154 ytytt 2
600 trtrtr 2
700 gfghf 2
If the Emp_Type was only one ie,
Select * from EMP where Emp_Type in (1) order by Emp_Type;
I know I can get it done by using TOP-N query method
select * from
(Select rownum RWnum ,A.* From
(Select * from EMP where Emp_Type in (1) order by Emp_Type) A
where rownum<=4)
where RWnum>=2;
but I am stuck up with doing for multiple Emp_Type..
here the Emp_Type could be more than two values…
Regards
Ben NarendranHi muthukumar S,dnikiforv,Vsugur
Thanks a lot for the analytical function ROW_NUMBER().
But still have some concern about the query you have given..
Let me first run the inner query.
SQL>
SQL > Select e.*,row_number() over(partition by Emp_Type order by Emp_Type) rno from
EMP e where Emp_Type in (1, 2 ) order by Emp_Type;
EMP_ID NAME EMP_TYPE RNO
154 aaa 1 1
101 dsds 1 2
122 gfgf 1 3
125 aaa 1 4
100 asas 1 5
700 gfghf 2 1
600 trtrtr 2 2
153 reree 2 3
If I give the range from 2 and 3 am suppose to get the following rows.
EMP_ID NAME EMP_TYPE RNO
101 dsds 1 2
122 gfgf 1 3
600 trtrtr 2 2
153 reree 2 3
when I run the query you sent am getting the following result..
SQL > select * from
(Select e.*,row_number() over(partition by Emp_Type order by Emp_Type) rno from
EMP e where Emp_Type in (1, 2 ) order by Emp_Type)
where rno between 2 and 3;
EMP_ID NAME EMP_TYPE RNO
101 dsds 1 2
154 aaa 1 3
600 trtrtr 2 2
700 gfghf 2 3
Here the company id 700 is not suppose to come, instead I have to get 153.
So I made some changes in the view and , wrote it like this
SQL> select * from
(select rno Rwnum,Outer_Qry.* from
(select Real_Qry.*,row_number() over(partition by emp_type order by emp_type)rno
from
(select emp_id,name,emp_type from emp where emp_type in (1,2) order by emp_type)Real_Qry)Outer_Qry
where rno <=3)
where Rwnum >=2;
RWNUM EMP_ID NAME EMP_TYPE RNO
2 101 dsds 1 2
3 122 gfgf 1 3
2 600 trtrtr 2 2
3 153 reree 2 3
I guess now am getting the correct result..
Please correct me if I am wrong, and please let me know if I have to make any modification on it..
Thanks and regards
Ben -
Index Usage in 9i changed when used rownum
Hi List,My Application is in RBO
Here is one more brain twister ?
Assume following I Have table SHRI with a single column
COL I have created Index on COL column say IND_COL.
Now when I run Following query on 8i with AUTOTRACE ON
I can see that 8i Uses the Index IND_COL Clearly .
select * from shri where (h_name='ABC' or h_name='BOM')
or (h_name ='BOM' or h_name='MOM')
and rownum=1
select * from shri where (COL='ABC' or COL='BOM')
or (COL='ABC' or COL='BOM')
and rownum=1
(Note:Above example is just for ur understanding plz.dont see the usage)
Now my problem is when I see the explain plan for above query in 8i I can see that Query uses IND_COL .
But when I run the same in 9i It does not uses IND_COL Index (Strange!!!) .
However when I remove "and rownum=1" clause then It again uses the index in 9i.
Assume that such queries are existing many places .
I am badly stuck because of this problem can anybody suggest the best way (Other than changing RBO to CBO) ?
Regards
Sripad.Run below query, you will get all the details of tablespaces.
col "Tablespace" for a22
col "Used MB" for 99,999,999
col "Free MB" for 99,999,999
col "Total MB" for 99,999,999
select df.tablespace_name "Tablespace",
totalusedspace "Used MB",
(df.totalspace - tu.totalusedspace) "Free MB",
df.totalspace "Total MB",
round(100 * ( (df.totalspace - tu.totalusedspace)/ df.totalspace))
"Pct. Free"
from
(select tablespace_name,
round(sum(bytes) / 1048576) TotalSpace
from dba_data_files
group by tablespace_name) df,
(select round(sum(bytes)/(1024*1024)) totalusedspace, tablespace_name
from dba_segments
group by tablespace_name) tu
where df.tablespace_name = tu.tablespace_name ; -
Issue regarding rownum in sql query
Hi All,
When I'm running the query below
select 'OP',
'ORG_CODE_PROVIDER',
rownum as ranking,
x.ORG_CODE_PROVIDER,
z.description,
x.value_count,
round(x.value_count / 200432, 4) * 100 as value_pct,
NULL as BATCH_KEY,
'9BED55A4328EFD71E040D20A143245E3' as BATCH_SET_KEY,
'OVERALL',
'OVERALL'
from (select ORG_CODE_PROVIDER, count(*) as value_count
from STAGING_TST.OP t
group by ORG_CODE_PROVIDER
order by count(*) desc, 1 asc) x,
(select code, description from ref_hd.MV_ORG_CODE_PROVIDER) z
where z.code(+) = x.ORG_CODE_PROVIDER
and rownum <= 10
it is showing me results based on the rownum of block x.
But when I try to insert these records in a table like
insert into QA_TST.OP_STAGE_COL_VAL_FREQ
select 'OP',
'ORG_CODE_PROVIDER',
rownum as ranking,
x.ORG_CODE_PROVIDER,
z.description,
x.value_count,
round(x.value_count / 200432, 4) * 100 as value_pct,
NULL as BATCH_KEY,
'9BED55A4328EFD71E040D20A143245E3' as BATCH_SET_KEY,
'OVERALL',
'OVERALL'
from (select ORG_CODE_PROVIDER, count(*) as value_count
from STAGING_TST.OP t
group by ORG_CODE_PROVIDER
order by count(*) desc, 1 asc) x,
(select code, description from ref_hd.MV_ORG_CODE_PROVIDER) z
where z.code(+) = x.ORG_CODE_PROVIDER
and rownum <= 10
On querying the table I'm getting totally different result based on the rownum governed by block y.
I could not able to understand why is it happening. Why oracle is not inserting the records that it is showing in select query.
Moreover, how can I fix this issue and get the desired result.
Thanks
TarunHi,
Whenever you post any code, indent it so that how it looks on the screen reflects what it is doing. In particular, make it easy to see what are the sub-queries. Whenever you post formatted text (such as query results as well as code) on this site, type these 6 characters:
\(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.
I originally posted an inaccurate answer becuase I couldn't understand your unformatted code.
How ROWNUM is assigned in a join depends on how the optimizer chooses to perform the join. If you want consistent results, then do the join first (in a sub-query), use ORDER BY clause in that sub-query, and use ROWNUM only in the parent query, which should not include a join.
The analytic ROW_NUMBER function is a lot more powerful and versatile than ROWNUM. You might look into using it (though the extra power may not be needed in this particular problem).
Edited by: Frank Kulash on Feb 10, 2011 11:29 AM -
How can I use the Rownum/Customized SQL query in a Mapping?
Hi,
* I need to use a Rownum for populating one of the target field? How to create a mapping with Rownum?
* How can I use an Dual table in OWB mapping?
* Can I write Customized SQL query in OWB? How can I achieve this in a Mapping?
Thanks in Advance
KishanHi Niels,
As I'm sure you know, the conundrum is that Reports doesn't know how many total pages there will be in the report until it is all done formatting, which is too late for your needs. So, one classical solution to this problem is to run the report twice, storing the total number of pages in the database using a format trigger, and throwing away the output from the first run when you don't know the total number of pages.
Alternatively, you could define a report layout so that the number of pages in the output is completely predictable based upon, say, the number of rows in the main query. E.g., set a limit of one, two, ... rows per page, and then you'll know how many pages there will be simply because you can count the rows in a separate query.
Hope this helps...
regards,
Stewart -
[b]how to use EJB QL for LIKE and ROWNUM[/b]
Hi,
i am using CMP EJB with OC4J server
i face two problems in it when i make any query like
select object(o) from Partymas o where o.stationCode like '?1' and o.rownum >='?2' and o.rownum <='?3' order by party_name
then it shows error on rownum so i cant understand how to solve it
second is when i use LIKE statment in EJB QL like this:-
select object(o) from Partymas o where o.pinCode like '%?%'
then it shows error so can any one help me
thanks
warm regards
vikassheelgupta<ejb-ql>select object(o) from Partymas o where o.stationCode like '?1' and
rownum >='?2' and rownum <='?3' order by party_name</ejb-ql>Try this:
rownum >= ?2 and rownum <= ?3
<ejb-ql>select object(o) from Partymas o where o.partyName like '%?%'</ejb-ql>Try this:
like ?1 -
How to have rownum in a report based on a view
How to use rownum in a report or tabular form?
I wanted to number the rows in a report. The most obvious way would be just stick
rownum in there. There is a view in a database1. It is merely select a,b,c from various
things joined together and order by pkey1.
Apex is in database2 so I have to create a view there across the link
like:
create or replace view SOME_VIEW as
select * from OTHER_VIEW@SOMELINK
alter view SOME_VIEW add constraint PK_SOME_VIEW
PRIMARY KEY (pkey1)
DISABLE NOVALIDATE;
The problem is if I try to include rownum in the query in the report I always get:
failed to parse SQL query:
ORA-01446: cannot select ROWID from, or sample, a view with DISTINCT, GROUP BY, etc.
There's no rowid, it knows that the primary key is Pkey1 so what is the problem?
I know it's rownum that it hates. Maybe I can just number these things some other
way. However it'd be good to figure out what it is griping about.
Anyone know?
Edited by: lake on Mar 12, 2011 9:36 AMthanks!
Actually I made a mistake in testing. And it does work to do this:
in database1:
create or replace view VIEW1 as
select
rownum "SEQ", etc.
in database2:
create view VIEW2 as select * from VIEW1@mylink
How I screwed up testing that I don't know. I was calling the column NUM before....
this is good because you can create a report about the next so many of something, such as how many of
the next batch of mailings have address problems? For that you need to know in what order it will be done and
when to quit.
But I'm sure I will use the report rownum thing also. thanks very much. -
How to determine the ROWNUM in a Master Detail Form
Hello,
I am working in a Master Detail Form with Orders and Order Items Information; and I created a process that allows the users to select a Product ID on a tabular form, and the next column is populated automatically with the Product Description.
To achieve that, I followed some instructions by Denes Kubicek (http://apex.oracle.com/pls/otn/f?p=31517:241), but my case is slightly different:
<ul>I am using a wizard-created tabular form, instead of a manual tabular form; and </ul>
<ul>Instead of using another page as a Popup to pass the parameter of the ROWNUM, I am using a plug-in called “Tabular Form Super LOV” which is a modified version of the SkillBuilders’ Super LOV that works on Tabular Forms. </ul>
The Product ID is returned in the correct row by the plug-in; but in the case of the Product Description, it is always returned into the first row; and my problem is that I don't know how to determine the row where the user clicked to call the plug-in.
I set up an example in apex.oracle.com in case someone would like to look at it:
Workspace: ediazjorge
Username: test
Password: test
App Name: Sample App
App Number: 1550
BTW, I am using Apex 4.1.1.00.23 and Oracle DB 10.3.2.
Thanks in advance,
ErickHello VC,
Thank you for your time and help.
Unfortunately, it is still not working properly:
1. When the Popup LOV opens, the first row of the tabular form is 0, the second row is 1, and so on. How can I assign the values starting with 1 and in the format *0001* ? (I am still a novice on Apex and new to JavaScript :-), sorry about that).
--2. Also, when I close the Popup LOV, the value of P1_ROWNUM is ‘undefined’ again so I'm not sure if the Product Name column will be populated.--
3. And finally, you are using the jQuery Selector uPopupLOVIcon, which is an attribute of the Cloudy Theme. In my real application (Apex 4.1.1.00.26), my element looks like: *<img src="/i/lov_16x16.gif" width="16" height="16" alt="Popup Lov" alt="List" style="vertical-align:middle;" align="middle" />*. What do you recommend me to use as a jQuery Selector?
Again, I really appreciate your help and time. I think you solved the most difficult part.
Thank you so much,
Erick
Update: I just found out that the problem with my comment 2 is because I didn’t delete the previous dynamic action that set the value to P1_ROWNUM.
Edited by: ediazjorge on Sep 18, 2012 11:20 AM -
Issue with complete refresh on materialized view when using rownum
Hi all,
I had an issue with rownum when using complete refresh
I am using rownum when creating materialized view and it is generation correctly.But the issue was when i am refreshing the same ,rownum was not getting sorted in ascending order.
anyone had come across this scenariorownum is determined as the row is output, so "order by rownum" does literally nothing.
http://docs.oracle.com/cd/B19306_01/server.102/b14200/pseudocolumns009.htm -
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:14What 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 -
Trying to create a view which consistent of :
1. Data from a set of tables, implemented as a
separat VIEW VIEW1 with an unique key.
CREATE OR REPLACE VIEW1 AS
SELECT ukey,... FROM divtables;
2. An subtable refering to the unique key, may contain a number of occurences for each key. Now implemented as VIEW2 containing rownum.
CREATE OR REPLACE VIEW2 AS
SELECT ukey,ROWNUM rno, ... FROM subtab;
ROWNUM - How to get around this
When using SQL from VIEW2 for a specific key, then rownum is 1,2..., when using in VIEW2 then rownum seems to be numbered for the base table as such.
RESULTVIEW - How to implement
=============================
The RESULTVIEW should be used in an generic application, where one datarecord should be present for each unique key (used for "mailmerge").
Assumes that subtable only contains max 4 rows. Was planning to implements it as:
CREATE OR REPLACE RESULTVIEW AS
SELECT ukey,...,
sum(decode(nvl(v2.rno,0),1,v2.val,0)) Val1,
max(decode(nvl(v2.rno,0),1,v2.desc,0)) Desc1,
sum(decode(nvl(v2.rno,0),4,v2.val,0)) Val4
max(decode(nvl(v2.rno,0),4,v2.desc,0)) Desc4,
FROM VIEW2 v2,VIEW1 v1
WHERE v2.ukey(+)=v1.ukey
GROUP BY ukey,...
The problem with rownum in VIEW2 makes the planned implementation impossible, any suggestions on how to get around this would be appreciated.
The result must be a view which contains only one record for each unique key, and may contains up to
four groups of values for subtable (5 values in each group).I suspect that you want VIEW2 to be defined as
CREATE OR REPLACE VIEW view2
AS
SELECT ukey,
ROW_NUMBER() OVER (PARTITION BY ukey ORDER BY <<something>>) rno
FROM subtabJustin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
Select a range of rows to be displayed using ROWNUM
I am trying to select a range of records to be displayed using Rownum
It works using MINUS
SQL> select rownum,department_id,department_name from departments where rownum <= 20
minus
select rownum,department_id,department_name from departments where rownum < 11;
but does not work if a range is specified
select rownum,department_id,department_name from departments where rownum >= 11 and rownum <= 20;
What has gone wrong?
Details of what I have tried are as follows:
Connect to the sample schema HR
SQL> connect hr/hr
SQL> desc departments
Name Null? Type
DEPARTMENT_ID NOT NULL NUMBER(4)
DEPARTMENT_NAME NOT NULL VARCHAR2(30)
MANAGER_ID NUMBER(6)
LOCATION_ID NUMBER(4)
List all records in Departments
SQL> select rownum,department_id,department_name from departments;
ROWNUM DEPARTMENT_ID DEPARTMENT_NAME
1 10 Administration
2 20 Marketing
3 30 Purchasing
4 40 Human Resources
etc......
26 260 Recruiting
27 270 Payroll
27 rows selected.
List the first 10 records in DEPARTMENTS
SQL> select rownum,department_id,department_name from departments where rownum <= 10;
ROWNUM DEPARTMENT_ID DEPARTMENT_NAME
1 10 Administration
2 20 Marketing
etc.....
10 100 Finance
List row number from 11 to 20, but cannot no rows selected. Why?
SQL> select rownum,department_id,department_name from departments where rownum >= 11 and rownum <= 20;
no rows selected
Use of MINUS can retrieve row number from 11 to 20
SQL> select rownum,department_id,department_name from departments where rownum <= 20
minus
select rownum,department_id,department_name from departments where rownum < 11;
ROWNUM DEPARTMENT_ID DEPARTMENT_NAME
11 110 Accounting
12 120 Treasury
13 130 Corporate Tax
14 140 Control And Credit
15 150 Shareholder Services
16 160 Benefits
17 170 Manufacturing
18 180 Construction
19 190 Contracting
20 200 Operations
10 rows selected.For each row returned by a query, the ROWNUM pseudocolumn returns a number indicating the order in which Oracle selects the row from a table or set of joined rows. The first row selected has a ROWNUM of 1, the second has 2, and so on.
Conditions testing for ROWNUM values greater than a positive integer are always false. For example, this query returns no rows:
SELECT * FROM employees
WHERE ROWNUM > 1;
You can get the selected records based on the rownum using the inline query....
SQL> select rownum, empno from emp;
ROWNUM EMPNO
1 7369
2 7499
3 7521
4 7566
5 7654
6 7698
7 7782
8 7788
9 7839
10 7844
11 7876
ROWNUM EMPNO
12 7900
13 7902
14 7934
14 rows selected.
SQL> select * from (select rownum rn, empno from emp) where rn > 2 and rn < 5;
RN EMPNO
3 7521
4 7566 -
Returning a subset of rows through a ResultSet WITHOUT rownum?
I am trying to figure out a way to get a subset of rows from a ResultSet (or ResultSet equivalent) without using rownum and without sending all the rows before the requested row over the wire.
My problem is this: I am not in control of the SQL that is sent to the database. It is autogenerated by a third party tool (TOPLink). Therefore using the rownum solution isn't practical. My steps of operation are:
1. Get SQL from TOPLink
2. Pass SQL through a Statement
3. Get resulting ResultSet
At this point, I want to say, "Mr. ResultSet, I only want rows 200-250." And I then want Mr. ResultSet to fetch only rows 200 to 250, and no others. I don't want to see rows 1 to 199 coming over the wire.
Anyone have any ideas? Is this possible at all? Also, if there's a way to do this through the TOPLink mechanisms (ScrollableCursors and ReportQuery objects and whatnot), please do let me know. I have tried using the TOPLink facilities, and sure enough rows 1 to 199 flow across the network.
Thanks,
Michael Allen
[email protected]Hi Michael,
Is there some sort of selection criteria in which you could use return the required 200-250 rows of data? If so, I would embed that into the TopLink query.
Another option is using the ORDER BY operator. Without order, constructing a query of elements 200-250 twice would not guarentee that they would be the same elements.
You can also construct your own SQL statement and execute it directly on a TopLink session.
Darren Melanson
Strategic Implementation Consultant
Oracle Canada.
I am trying to figure out a way to get a subset of rows from a ResultSet (or ResultSet equivalent) without using rownum and without sending all the rows before the requested row over the wire.
My problem is this: I am not in control of the SQL that is sent to the database. It is autogenerated by a third party tool (TOPLink). Therefore using the rownum solution isn't practical. My steps of operation are:
1. Get SQL from TOPLink
2. Pass SQL through a Statement
3. Get resulting ResultSet
At this point, I want to say, "Mr. ResultSet, I only want rows 200-250." And I then want Mr. ResultSet to fetch only rows 200 to 250, and no others. I don't want to see rows 1 to 199 coming over the wire.
Anyone have any ideas? Is this possible at all? Also, if there's a way to do this through the TOPLink mechanisms (ScrollableCursors and ReportQuery objects and whatnot), please do let me know. I have tried using the TOPLink facilities, and sure enough rows 1 to 199 flow across the network.
Thanks,
Michael Allen
[email protected] -
Needs help to retrive the last row in a select query without using rownum
Hi ,
i need to retrive the last row from the select sub query without using rownum.
is there any other way to retrive the last row other than the below query.
is that the ROWNUM=1 will always retrive the 1 row of the select query ?
select from*
*(select ename from employee where dept_id=5 order by desc) where rownum=1;*
Please advise.
thanks for your help advance,
regards,
Senthur957595 wrote:
Actually my problem is ithat while selecting the parents hiearchy of the child data using
CONNECT BY PRIOIR query
I need the immediate parent of my child data.
For example my connect BY query returns
AAA --- ROOT
BBB --PARENT -2
CCC --PARENT-1
DDD IS my input child to the connect by query
Immediate parent of my child data "DDD" ---> CCC(parent -1)
i want the data "CCC" from the select query,for that i am taking the last row of the query with rownum.
I got to hear that using ROWNUM to retrive the data will leads to some problem.It is a like a magic number.I am not sure what the problem will be.
So confusing with using this rownum in my query.
Please advice!!!It's not quite clear what you're wanting, but perhaps this may help?
you can select the PRIOR values to get the parent details if you want...
SQL> ed
Wrote file afiedt.buf
1 select empno, lpad(' ',(level-1)*2,' ')||ename as ename, prior empno as mgr
2 from emp
3 connect by mgr = prior empno
4* start with mgr is null
SQL> /
EMPNO ENAME MGR
7839 KING
7566 JONES 7839
7788 SCOTT 7566
7876 ADAMS 7788
7902 FORD 7566
7369 SMITH 7902
7698 BLAKE 7839
7499 ALLEN 7698
7521 WARD 7698
7654 MARTIN 7698
7844 TURNER 7698
7900 JAMES 7698
7782 CLARK 7839
7934 MILLER 7782
14 rows selected.(ok, not the best of examples as the mgr is already known for a row, but it demonstrates you can select prior data) -
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 -
What are ROWID and ROWNUM? Are they stored in database and where?
Hi All,
can anybody please answer this question
What are ROWID and ROWNUM? Are they stored in database and where?
Thanks,
SriniROWID can be thought of as a pointer to the physical location (on disk) of the (table) row.
From a ROWID value, Oracle can extract the file, block-within-that-file and offset-of-the-row-within-that-block. Using these, Oracle can directly access a disk block to retrieve a row.
ROWNUM is a just sequence number of a row within a result set of a query.
As said by other repliers, both are not stored. They are 'constructed' when you reference them inside a query.
Maybe you are looking for
-
I need help with this tutorial from the Adobe website
I'm almost done with my flash game which I created with flash pro cc. And i have been looking for tutorials to integrate my game to the Facebook and this is the only one I found. http://www.adobe.com/devnet/games/articles/getting-started-with-faceboo
-
PrePro [CS4] error because of missing plug-in
Hi there, I got a problem when starting Premiere Pro CS 4.2.1. The thing is that I deleted a plug-in that was connected with Premiere Pro. A few days ago when I upgraded to Apple Logic Studio 2 I decided to get rid of all everything I don't need anym
-
I bought LR 5 Oct 16 this year. Why doesn't my watermark work when I export photos? I followed tutorials and still it does not work. Once, a few photos out of about 100 exported with the watermark, but only once. Is this a bug? how to fix? Also, how
-
Veromix plasmoid keeps not working
The last time I have tried veromix I had a problem that sometimes it would somehow "lose" the connection to pulseaudio and just don't update the outputstreams any more or just flatout not displaying anything anymore. Usually killing plasma and restar
-
How to create background based off a video?
Hi guys, I'm new to the scene and it's hard to explain this question, but how do you do something like this?? you can see this was taken on an iPhone or a similar device, and originally the video would consist of the middle section, but now it's been