Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Mailing Lists -> Oracle-L -> RE: Top N query - RANK() and DENSE_RANK()
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
------_=_NextPart_001_01C07B49.28BC623C
Content-Type: text/plain;
charset="iso-8859-1"
Thanks Djordje, but actually that isn't exactly what I'm looking for.
The RANK() and DENSE_RANK() functions do what I need, I got it working. Has anyone used these in a large environment? Are they performance killers?
Thanks
-----Original Message-----
From: Djordje Jankovic [mailto:djankovic_at_corp.attcanada.ca]
Sent: Wednesday, January 10, 2001 3:48 PM
To: 'ORACLE-L_at_fatcity.com'
Cc: 'lkoivu_at_qode.com'
Subject: RE: Top N query
This should do the job:
select category, num
from tab a
where 3 > (select count(*)
from tab b where a.category = b.category and a.num < b.num)
HTH Djordje
-----Original Message-----
From: Koivu, Lisa [mailto:lkoivu_at_qode.com]
Sent: Wednesday, January 10, 2001 3:01 PM
To: Multiple recipients of list ORACLE-L
Subject: Top N query
OK, I apologize in advance, I know this has been discussed many times.
I need to generate the top N, in this case 3, values for a query, like the example shown below. It has to be done in SQL, because I need to return this as a refcursor. (OPEN refcurcor FOR...)
category1 10 category1 14 category1 2 category1 25 category2 14 category2 13 category2 55
I need to display this:
category1 25
category1 14
category1 10
category2 55
category2 14
category2 13
category3 14
category3 1
category4 5
Can you hear me cursing....
Thanks in advance for any suggestions.
Lisa Rutland Koivu
Oracle Database Administrator
Qode.com
4850 North State Road 7
Suite G104
Fort Lauderdale, FL 33319
V: 954.484.3191, x174 F: 954.484.2933 C: 954.658.5849
"The information contained herein does not express the opinion or position of Qode.com and cannot be attributed to or made binding upon Qode.com."
------_=_NextPart_001_01C07B49.28BC623C
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2650.12">
<TITLE>RE: Top N query - RANK() and DENSE_RANK()</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=3D2>Thanks Djordje, but actually that isn't exactly what =
I'm looking for. </FONT>
</P>
<P><FONT SIZE=3D2>The RANK() and DENSE_RANK() functions do what I need, =
I got it working. Has anyone used these in a large =
environment? Are they performance killers? </FONT></P>
<P><FONT SIZE=3D2>Thanks</FONT>
</P>
<BR>
<P><FONT SIZE=3D2>-----Original Message-----</FONT>
<BR><FONT SIZE=3D2>From: Djordje Jankovic [<A =
HREF=3D"mailto:djankovic_at_corp.attcanada.ca">mailto:djankovic_at_corp.attcan=
ada.ca</A>]</FONT>
<BR><FONT SIZE=3D2>Sent: Wednesday, January 10, 2001 3:48 PM</FONT>
<BR><FONT SIZE=3D2>To: 'ORACLE-L_at_fatcity.com'</FONT>
<BR><FONT SIZE=3D2>Cc: 'lkoivu_at_qode.com'</FONT>
<BR><FONT SIZE=3D2>Subject: RE: Top N query</FONT>
</P>
<BR>
<P><FONT SIZE=3D2>This should do the job:</FONT>
<BR><FONT SIZE=3D2> </FONT>
<BR><FONT SIZE=3D2>select category, num</FONT>
<BR><FONT SIZE=3D2> from tab a</FONT>
<BR><FONT SIZE=3D2> where 3 > (select count(*)</FONT>
<BR><FONT =
SIZE=3D2> &nb=
sp; from tab b</FONT>
<BR><FONT =
SIZE=3D2> &nb=
sp; where a.category =3D b.category</FONT>
<BR><FONT =
SIZE=3D2> &nb=
sp; and a.num < b.num)</FONT>
<BR><FONT SIZE=3D2> order by 1 asc, 2 desc</FONT>
<BR><FONT SIZE=3D2>/</FONT>
<BR><FONT SIZE=3D2> </FONT>
<BR><FONT SIZE=3D2>HTH</FONT>
<BR><FONT SIZE=3D2> </FONT>
<BR><FONT SIZE=3D2>Djordje</FONT>
</P>
<BR>
<P><FONT SIZE=3D2>-----Original Message-----</FONT>
<BR><FONT SIZE=3D2>From: Koivu, Lisa [<A =
HREF=3D"mailto:lkoivu_at_qode.com">mailto:lkoivu_at_qode.com</A>]</FONT>
<BR><FONT SIZE=3D2>Sent: Wednesday, January 10, 2001 3:01 PM</FONT>
<BR><FONT SIZE=3D2>To: Multiple recipients of list ORACLE-L</FONT>
<BR><FONT SIZE=3D2>Subject: Top N query</FONT>
</P>
<BR>
<BR>
<P><FONT SIZE=3D2>OK, I apologize in advance, I know this has been =
discussed many times. </FONT>
</P>
<P><FONT SIZE=3D2>I need to generate the top N, in this case 3, values =
for a query, like the</FONT>
<BR><FONT SIZE=3D2>example shown below. It has to be done in SQL, =
because I need to return</FONT>
<BR><FONT SIZE=3D2>this as a refcursor. (OPEN refcurcor FOR...) =
</FONT>
</P>
<P><FONT SIZE=3D2>category1 10 </FONT>
<BR><FONT SIZE=3D2>category1 14 </FONT>
<BR><FONT SIZE=3D2>category1 2 </FONT>
<BR><FONT SIZE=3D2>category1 25 </FONT>
<BR><FONT SIZE=3D2>category2 14 </FONT>
<BR><FONT SIZE=3D2>category2 13 </FONT>
<BR><FONT SIZE=3D2>category2 55 </FONT>
<BR><FONT SIZE=3D2>category2 5 </FONT>
<BR><FONT SIZE=3D2>category3 14 </FONT>
<BR><FONT SIZE=3D2>category3 1 </FONT>
<BR><FONT SIZE=3D2>category4 5 </FONT>
</P>
<P><FONT SIZE=3D2>I need to display this: </FONT>
</P>
<P><FONT SIZE=3D2>category1 25 </FONT>
<BR><FONT SIZE=3D2>category1 14 </FONT>
<BR><FONT SIZE=3D2>category1 10 </FONT>
<BR><FONT SIZE=3D2>category2 55 </FONT>
<BR><FONT SIZE=3D2>category2 14 </FONT>
<BR><FONT SIZE=3D2>category2 13 </FONT>
<BR><FONT SIZE=3D2>category3 14 </FONT>
<BR><FONT SIZE=3D2>category3 1 </FONT>
<BR><FONT SIZE=3D2>category4 5 </FONT>
</P>
<P><FONT SIZE=3D2>Can you hear me cursing.... </FONT>
</P>
<P><FONT SIZE=3D2>Thanks in advance for any suggestions. </FONT>
</P>
<P><FONT SIZE=3D2>Lisa Rutland Koivu </FONT>
<BR><FONT SIZE=3D2>Oracle Database Administrator </FONT>
<BR><FONT SIZE=3D2>Qode.com </FONT>
<BR><FONT SIZE=3D2>4850 North State Road 7 </FONT>
<BR><FONT SIZE=3D2>Suite G104 </FONT>
<BR><FONT SIZE=3D2>Fort Lauderdale, FL 33319 </FONT>
</P>
<P><FONT SIZE=3D2>V: 954.484.3191, x174 </FONT>
<BR><FONT SIZE=3D2>F: 954.484.2933 </FONT>
<BR><FONT SIZE=3D2>C: 954.658.5849 </FONT>
<BR><FONT SIZE=3D2><A HREF=3D"http://www.qode.com" =
TARGET=3D"_blank">http://www.qode.com</A> <<A =
HREF=3D"http://www.qode.com" =
TARGET=3D"_blank">http://www.qode.com</A>> </FONT>
</P>
<P><FONT SIZE=3D2>"The information contained herein does not =
Received on Wed Jan 10 2001 - 15:06:01 CST
![]() |
![]() |