ROWNUM and order of elements in PL/SQL collection
I have collection col of type tt_number is table of number.
For example, values in collection are:
col[1] = 123
col[2] = -123
col[3] = 999
I'm running such query:
select ROWNUM as "rn", column_value as "val"
from table(col)
Could I be sure, that my results always will look like:
RN VAL
1 123
2 -123
3 999
Does ROWNUM equals to collection index?
As I know, when querying tables, ROWNUM doesn't guarantee such things (unless querying "ordered by" subquery). But how about queries to collections?
Regards, Dmitry Kuitskiy, 3-T Ltd, Kiev, Ukraine
These Tom Kyte's threads can help you:
http://asktom.oracle.com/pls/ask/f?p=4950:8:1193224283514449959::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:4447489221109
http://asktom.oracle.com/pls/ask/f?p=4950:8:1193224283514449959::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:3008276249843
Rgds.
Similar Messages
-
Rownum and order by as parameter
i want a procedure that will accept the 'from' and 'to' parameter (rownum) for paginaton as well as the order by column also as a parameter( the order by changes based on the parameter) , and my query is using multiple table which doesnt have Unique keys, the pagination is not working poperly at that time..
please have a look in to the procedure..
CREATE OR REPLACE Procedure P_pagination
(cur out sys_refcursor,end1 number,start1 number,ordr number)
as
Var_sql varchar2(4000);
begin
var_sql := ' Select * '||
' From '||
' (select rownum rwnum,aa.* from ' ||
' (select ti.a,t2.a,t3.a,t4.b from t1,t2,t3,t4 where < all the joins> order by '||ordr||' )aa'||
' where rownum <='|| end1 ||') ' ||
' where rwnum >='|| start1 ;
open cur for var_sql;
end ;
/If you don't specify explicit ORDER BY clause
Oracle wont guarantee the order of the sort. You specify ORDER BY ID , but
not ORDER BY ID, <<something>> , so you have unrepeatable results. Your order by ID but don't order inside ID. Look:
SQL> select *
2 from
3 (select a.*, rownum rnum
4 from
5 (select id, data
6 from t
7 order by id) a
8 where rownum <= 150
9 )
10 where rnum >= 148
11 /
ID DATA RNUM
0 34 148
0 54 149
0 81 150
SQL> select *
2 from
3 (select a.*, rownum rnum
4 from
5 (select id, data
6 from t
7 order by id) a
8 where rownum <= 151
9 )
10 where rnum >= 148
11 /
ID DATA RNUM
0 28 148
0 34 149
0 54 150
0 81 151
SQL> select *
2 from
3 (select a.*, rownum rnum
4 from
5 (select id, data
6 from t
7 order by id, rowid) a
8 where rownum <= 150
9 )
10 where rnum >= 148
11 /
ID DATA RNUM
0 24 148
0 21 149
0 35 150
SQL> select *
2 from
3 (select a.*, rownum rnum
4 from
5 (select id, data
6 from t
7 order by id, rowid) a
8 where rownum <= 151
9 )
10 where rnum >= 148;
ID DATA RNUM
0 24 148
0 21 149
0 35 150
0 68 151Rgds. -
Trouble with subquery and rownum and ordering
I'm having trouble making this subquery work. The basic idea is that the web app will show only so many rows of results on the page, but right now I'm just playing around in SQL*Plus. The address book holds mixed case, so in order to sort by name properly I need to use UPPER to ignore case sensitivity. This SQL statement works fine for me:
select addressbookid from addressbook where addressbookid like '905430931|%' order by upper(addressbookid);I know that if you mention ROWNUM in the WHERE clause, you're referring to the row numbers of the ResultSet that Oracle returns. How do I use both ORDER BY UPPER(ADDRESSBOOKID) and WHERE ROWNUM > 25 AND ROWNUM <= 50? I figured a subquery would do it, but I can't write a correct one that does it! Below are my 2 attempts with the errors, and then I just tried to play with rownum:
SQL> select addressbookid from addressbook where addressbookid in (select addressbookid from address book where addressbookid like '905430931|%' order by upper(addressbookid));
select addressbookid from addressbook where addressbookid in (select addressbookid from addressbook
ERROR at line 1:
ORA-00907: missing right parenthesis
SQL> select addressbookid from addressbook where addressbookid in upper(select addressbookid from addressbook where addressbookid like '905430931|%');
select addressbookid from addressbook where addressbookid in upper(select addressbookid from address
ERROR at line 1:
ORA-00936: missing expression
SQL> select addressbookid from addressbook where addressbookid like '905430931|%' and rownum > 25 and rownum <= 50 order by upper(addressbookid);
no rows selected
SQL> select addressbookid from addressbook where addressbookid like '905430931|%' and rownum > 25 and rownum <= 50;
no rows selectedLike I said, if I can get a working subquery, then I'd like to attach that restriction on the rownum stuff. I'm wondering if it will be a problem trying to get the ordering to happen and then the rownum restriction after that, and all in the same query...
Btw, we've made all the table and column names in our database uppercase, so that shouldn't matter.This is probably the most efficient way ...
select addressbookid
from
select addressbookid,
rownum rn
from
select addressbookid
from addressbook
where addressbookid like '905430931|%'
order by addressbookid
where rownum <= 50
where rn > 25 -
Combination of rownum and order by in where
Hi friends
This is the simple query pl. have a look.
SQL> select distinct sal from emp
2 order by sal desc;
SAL
5000
3000
2975
2850
2450
1600
1500
1300
1250
1100
950
SAL
800
500
200
14 rows selected.
Now I want to list only top 5 salaries from this with sql statement
like
5000
3000
2975
2850
2450This feature is available from oracle's 8.1.5 onwards
Example with an Inline View
Suppose we have a table emp which contains empname and sal. then using Inline View we can write sql like this:
SELECT * FROM (SELECT * FROM emp ORDER BY sal DESC)
WHERE ROWNUM < 5;
here u will get the top 4 salaries.if u want to avoid same sal then use distict also.If u want 2nd maxima then put rownum=2;
I think u r tried with oracle 8.0 or below versions.test with oracle 8i or above.Then u will get the result.
Best wishes
SHINOY.V.V.
KODUNGALLUR,
THRISSUR
KERALA
INDIA
( WORKING IN[b] IBM BANGALORE)
Kerala,Thrissur,Kodungallur)
IBM Bangalore -
How to : rownum and order by in select statement
Say I have a select statement with additional 'order by' clause.
The result set I want prepended by the rownum column.
But, and here comes the flaw, I want the rownum for the already ordered result set
not the unordered.
An example:
select firstname, lastname from myTable order by lastname;
When I add the rownum to the select clause,
'select rownum, firstname, lastname from myTable order by lastname;'
I might get something like:
20 Mike Adams
13 Nina Bravo
1 Tom Charlie
But I want the following result:
1 Mike Adams
2 Nina Bravo
3 Tom Charlie
I could now
'Select rownum, lastname, firstname from (select firstname, lastname from myTable order by lastname);
But I guess there is a better way!?!
which is the best way to accomplish that?
Thanks for your advice!>
'Select rownum, lastname, firstname from (select firstname, lastname from myTable order by lastname)
>
Well if you ask me there is very little difference between this query and the above query
select rownum, lastname, firstname from mytable;Because rownum is assigned before the order by. The difference is in your query you are assigning a rownum to an ordered resultset but still there is no guarantee oracle is going to read the data in an ordered fashion. In the second query rownum is assigned to an unordered resultset. Again it is the samething. So if you want to guarantee it then I will go for the following option
select row_number() over(order by lastname) rn, lastname, firstname from mytable
order by lastnameAlso check this link.
http://www.oracle.com/technology/oramag/oracle/07-jan/o17asktom.html
Regards
Raj
Edited by: R.Subramanian on Jan 13, 2009 6:20 AM -
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>0But,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>0Please 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 -
How to iterate xml elements using PL/SQL
Hello,
Let's say I have the following xml:
<A>
<B name="b1">
</B>
<B name="b2">
</B>
</A>
I would like to iterate over the B and sub-B elements using PL/SQL?
Any help will be appreciated.Hi,
You can use 'PL/SQL DOM API for XMLType (DBMS_XMLDOM)'
to work with XML. You can check the example present at
http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/appdev.920/a96620/xdb08pls.htm#1041419
to check how it does.
The API for the package DBMS_XMLDOM is given on the same page(http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/appdev.920/a96620/xdb08pls.htm#1040676)
You can use them for your requirement.
Regards,
Anupama -
Will the order of elements stored in pl.sql table retains
Hello Friends,
I am having a record type and the for each element of record , i am having corresponding pl.sql table type .
If i am storing the values into the records from a query and also the individual elements in the pl.sql table type will the order of data is stored as it is . ..
example...
in a record type the data is stored as ( name1 , age1 , salary1) , (name2, age2, salary2)
if i store in corresponding pl sql table type name1 , name2
age1, age2
salary1, salary2
can i relate the index of record type with that of pl/sql table type ..
pls advice
thanks/kumarKumar,
Yes, the order of elements will be the same.
Any specific reason why you would want to create a collection for each individual attribute of the record ?
You can as well declare another variable of the same record and initialize it.
A few other suggestions for your code :
1) The 2nd FOR loop can be modified to accommodate the query of the first cursor there-by eliminating one extra iteration.
SELECT MINC.FAMID,
MINC.MEMBNO,
MEMB.AGE,
SALARYX,
SALARYBX,
NONFARMX,
NONFRMBX,
FARMINCX,
FRMINCBX,
CU_CODE
FROM MINC, MEMB, FMLY
WHERE MINC.FAMID = MEMB.FAMID
AND MINC.MEMBNO = MEMB.MEMBNO
AND MINC.FAMID = FMLY.FAMID
AND MEMB.FAMID = FMLY.FAMID
ORDER BY MINC.FAMID2) The collections can be alternately initialized as follows :
v_member_rec(v_member_rec.last).FAMID := j.FAMID;
max_earnings_tab(max_earnings_tab.last) := v_max_earnings;The tried the below example for confirmation ...
declare
type emp_rec is record
(name emp.ename%TYPE,
dept emp.edept%TYPE,
sal emp.esal%TYPE
TYPE emp_rec_tab is table of emp_rec;
ert emp_rec_tab := emp_rec_tab();
TYPE ename_tab is table of varchar2(20);
ent ename_tab := ename_tab();
TYPE edept_tab is table of number;
edt edept_tab := edept_tab();
begin
for i in (select * from emp)
loop
ert.extend;
ent.extend;
edt.extend;
ert(ert.last).name := i.ename;
ert(ert.last).dept := i.edept;
ert(ert.last).sal := i.esal;
ent(ent.last) := i.ename;
edt(edt.last) := i.edept;
end loop;
for i in 1..ert.count
loop
dbms_output.put_line(ert(i).name||','||ent(i));
dbms_output.put_line(ert(i).dept||','||edt(i));
dbms_output.put_line('');
end loop;
end; -
Report by Internal Order that includes cost element and order type
Good Morning Gurus'
Is there an SAP Standard Report that shows Internal order, cost element and order type? I find many with order, but not order type.
Thanks a million!!Hi,
No, I don't think so. You can create a workaround by defining order group based on order type by intervals. Or, of course, develop your own report.
Regards,
Eli -
How to export an album and keep images in the same order in elements 11 mac
I made an album of my trip to Australia (lots of pictures) and changed the order of the images (not the same as shooting order) and when I export the images in the album to a folder or memorystick, the images are copied to that place but I lost the album order of the images.
How can I prevent that, without having to renumber al 500 images separatly?
Gr JosThanks for the tip, Michel
It worked like a charm…
cheers Jos
Op 28 okt. 2013, om 18:49 heeft MichelBParis <[email protected]> het volgende geschreven:
Re: How to export an album and keep images in the same order in elements 11 mac
created by MichelBParis in Photoshop Elements - View the full discussion
JosU a écrit:
I made an album of my trip to Australia (lots of pictures) and changed the order of the images (not the same as shooting order) and when I export the images in the album to a folder or memorystick, the images are copied to that place but I lost the album order of the images.
How can I prevent that, without having to renumber al 500 images separatly?
Gr Jos
The order in album is only kept in the organizer database, so the only way not to lose that order is to rename files.
If you are using the 'export' function, select your album in custom order, select all photos and in the dialog, choose to rename with a common base name.
Please note that the Adobe Forums do not accept email attachments. If you want to embed a screen image in your message please visit the thread in the forum to embed the image at http://forums.adobe.com/message/5794538#5794538
Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page: http://forums.adobe.com/message/5794538#5794538
To unsubscribe from this thread, please visit the message page at http://forums.adobe.com/message/5794538#5794538. In the Actions box on the right, click the Stop Email Notifications link.
Start a new discussion in Photoshop Elements at Adobe Community
For more information about maintaining your forum email notifications please go to http://forums.adobe.com/message/2936746#2936746. -
EA1 - SQL Formatter issues (JOINs and GROUPs and ORDER BY oh my ;)
Great job with improving the SQL Formatter, but it still has some bugs that need to be worked out.
The key words JOIN and it's modifiers INNER, LEFT, RIGHT and FULL OUTER are not recognized as master key words. As such they end up flush against the left margin Also when GROUP BY and/or ORDER BY key words are present in an outer most select statement the other key words are not indented far enough to be right aligned with the end of the word BY and are indented too far to be right aligned with the word GROUP or ORDER. In sub queries, GROUP and ORDER BY are correctly right aligned with their respective SELECT statements.We're picking up and collating the Formatter issues. I'll add these.
Specific bug for these #7013462
Sue -
Have ordered Photoshop Elements 12 and Premiere Elements 12 (for MAC, EN version). Order went through but finally order was cancelled as Adobe Store was unable to approve. Subsequently order was cancelled. did anybody experience the same issue and how to proceed.
Hi there
Please check with your credit card issuer to see why payment is not being approved. When this is resolved you should be able to place a new order.
Thanks
Bev -
Question about order by and rownum and subqueries.
Can you explain why test 4 below results as it results- with returning no rows?
Identificator "PKG_INTRA_CUSTOMER_SEARCH.NAME" is a package function, which returns value from a variable declared in package body, the function returns value 'e%'.
Please note that tests 1-3 all returned data, but test 4 didn't. Why? My porpouse is that test 4 would also return data.
1. Query without "rownum" and with "Order by" returns 2 records:
select q.*--, rownum
,PKG_INTRA_CUSTOMER_SEARCH.NAME
from
select c.ID,
c.NAME,
c.CODE
from V_CUSTOMER c
where 1=1 and lower(c.NAME) like PKG_INTRA_CUSTOMER_SEARCH.NAME
order by c.NAME, c.ID
) q
10020 Ees Nimi 37810010237 e%
10040 ewewrwe werwerwer e%2. Query with "rownum" and without "Order by" returns 2 records:
select q.*, rownum
,PKG_INTRA_CUSTOMER_SEARCH.NAME
from
select c.ID,
c.NAME,
c.CODE
from V_CUSTOMER c
where 1=1 and lower(c.NAME) like PKG_INTRA_CUSTOMER_SEARCH.NAME
--order by c.NAME, c.ID
) q
10020 Ees Nimi 37810010237 e%
10040 ewewrwe werwerwer e%3.Query without "rownum" and with "Order by" returns 2 records:
select q.*--, rownum
,PKG_INTRA_CUSTOMER_SEARCH.NAME
from
select c.ID,
c.NAME,
c.CODE
from V_CUSTOMER c
where 1=1 and lower(c.NAME) like PKG_INTRA_CUSTOMER_SEARCH.NAME
order by c.NAME, c.ID
) q
10020 Ees Nimi 37810010237 e%
10040 ewewrwe werwerwer e% 4. Query with "rownum" and with "Order by" returns 0 records:
select q.*, rownum
,PKG_INTRA_CUSTOMER_SEARCH.NAME
from
select c.ID,
c.NAME,
c.CODE
from V_CUSTOMER c
where 1=1 and lower(c.NAME) like PKG_INTRA_CUSTOMER_SEARCH.NAME
order by c.NAME, c.ID
) qHi,
please reproduce the question in your test database with script below.
My general question is that wht Test5 below returns only 1 row, but Test5 returns 3 rows.
The difference between those two tests is only that one has "order by" clause, the other doesn't have.
I need the "order by" clause to stay, but seems like it is not allowed.
CREATE OR REPLACE
PACKAGE PACKAGE1 AS
function NAME return varchar2;
END PACKAGE1;
CREATE OR REPLACE
PACKAGE body PACKAGE1 AS
function NAME return varchar2
is
begin
return 'e%';
end NAME;
END PACKAGE1;
select PACKAGE1.name from dual--e%
create table Tbl AS
(SELECT 'e2b' Col1, 'A' Col2, 'AA' Col3 FROM dual
UNION
SELECT 'e3b', 'B','BB' FROM dual
--Test5:
select q.*, rownum pos, PACKAGE1.name f
from
select c.col1,
c.col2,
c.col3
from Tbl c
where 1=1 and lower(c.col1) like PACKAGE1.name
order by c.col2, c.col1
) q
union all
select '111' , '111' , '111' , 0 pos, PACKAGE1.name f from dual --return 1 row
--Test6
select q.*, rownum pos, PACKAGE1.name f
from
select c.col1,
c.col2,
c.col3
from Tbl c
where 1=1 and lower(c.col1) like PACKAGE1.name
--order by c.col2, c.col1
) q
union all
select '111' , '111' , '111' , 0 pos, PACKAGE1.name f from dual --return 3 rowsEdited by: CharlesRoos on Feb 17, 2010 5:30 AM -
UK Sizes and ordering for photobooks in Elements 9
Hi,
I'm sorry to post this in the US section but there is no UK suppoer for Elements 9 on Adode's website.
I'm trying to create a photobook using Elements 9 but all the options have US sizes and ordering options (i.e. everything is in US dollars).
Is there any way to set up so that I can use UK sizes and UK printing companies?
Many thanks,
BenAre you sure you have the regular horizontal or vertical type tool and not one of the type masks selected? If you choose the type mask tools you see a dotted outline around the T icon instead of a solid T.
Maybe you are looking for
-
CD/DVD R discs won't play in Sony DVD player system.
I have just backed up my music and photos on CD R and DVD R discs on my new Macbook Pro. None will play on my Sony player. I previously purchased CD RW from Verbatim recorded on my Powerbook G4 and they play fine. I know there is technology change he
-
Unable to remove duplicate shared networks in finder
I have multiple entries for my WD MyBook Live under 'Shared'. They are displaying as 'Sharename', 'Sharename (1)', 'Sharename (2)', etc. I am using the MyBook Live for TimeMachine backups. I am attempting to access the media on the NAS through iTunes
-
How to add a glow to your highlights?
hello! does anyone know how to add a slight 'glow' or diffusion to the highlights only? i have been playing around with copying a clip. putting it above the original- adding a gaussion blur and boosting saturation and contrast etc then changing the o
-
LJ 4050 PCL 5, wired network, win8.1, very long between pages.
I have Dell Inspirion 15R with Win 8.1, printing to 4050 over wired network. Print speed per page is fine, but the printer will print 1-2 pages, then pause, then print some more. Only the "processing" msg on the printer tells me it is still workin
-
Screen brightness not changing
I recently formatted my system with windows 8 64-bit and after installing all the drivers, my screen brightness doesnt change