Top N values [message #370973] |
Thu, 27 July 2000 09:16 |
Jaro
Messages: 3 Registered: July 2000
|
Junior Member |
|
|
I have Oracle 7.3
I need to make select 4 highest values from the table, but these values are to be obtained as sum.
Select ID, Name, sum(sal) from my_table
gropu by ID, name
And I need just 4 highest records. (using SQL, not PL/SQL)
|
|
|
|
Re: Top N values [message #370982 is a reply to message #370973] |
Fri, 28 July 2000 12:37 |
Andrew again...
Messages: 270 Registered: July 2000
|
Senior Member |
|
|
If I understand the problem correctly, his should work fine:
1.) Create the query to return ID, Name, sum(sal)
2.) Query this result set to return just the top 4 records
Remember that a table and the result set from another query can be treated as being the same thing, see the following example:
Select My_Query.ID, My_Query.Name, My_Query.sum_sal
from
(select ID, Name, sum(Sal) sum_sal
from my_table
group by ID, name) My_Query
where ... rest of the top 4 syntax;
The My_Query alias is only there for clarity. Does this help.
|
|
|
|