Click to See Complete Forum and Search --> : Top 5 Priority Rows In order by?


aph
08-26-2004, 11:11 PM
Hi,
I have a requirement to print top 5 rows in a sequence based on the Priority_key. let's see we have table test_table with 2 columns below

Priority_key Code
1 A2
2 AA
4 A3
9 A1
6 A8
3 AZ
8 AX

The Output I want
1. Order by Priority_key
2. Only first 5 priority rows

Output
Priority_key Code
1 A2
2 AA
3 AZ
4 A3
6 A8

Please help to resolve this, thanks in advance.

hrishy
08-27-2004, 03:53 AM
Hi

Try

select priority_key,code
from (select priority_key,code ,rownum rn
from table
order by priority_key) X
where x.rn<6;

regards
Hrishy

aph
08-28-2004, 11:26 PM
Hi hrishy,
Thanks for the help but its not working according to my requirement.

Please consider the below values

Priority_key Code
1 A2
2 AA
4 A3
9 A1
6 A8
3 AZ
8 AX

and need the output as a
1 A2
2 AA
3 AZ
4 A3
6 A8


Thanks

jmodic
08-29-2004, 04:47 PM
If you would have taken a minute of your time and actualy run the query hrishy has suggested, you'd have found out that it is returning exactly the output that you want.....

aph
08-30-2004, 01:35 AM
I am sorry hrishy & jmodic, yes I was wrong it is working thanks. Now I need another help. What if I have a another table test_table2 and columns code & description and want to join with my test_table code with test_table2 code to get description.

TEST_TABLE
----------
Priority_key Code
1 A2
2 AA
4 A3
9 A1
6 A8
3 AZ
8 AX


TEST_TABLE2
-----------
Code Description
A2 Computer
AA Mouse
A3 Hard Disk
A1 keybord
A8 Memory
AZ Network Card
AX USB Port

Now based on the first table TEST_TABLE join column Code with TEST_TABLE2 column Code and print description but the same requirement 'Top 5 priority rows in order by'.

Thanks

zzz
08-30-2004, 03:10 AM
Try this query
SELECT A.priority_key, A.code, B.DESCRIPTION
FROM (select priority_key,code from (select priority_key,code ,rownum rn from T1
order by priority_key) X where x.rn<6) A,
T2 B
WHERE B.CODE = A.CODE
ORDER BY A.priority_key

aph
08-31-2004, 08:38 AM
Thanks it works perfect!!!