I have the testmax table as following :
I J
---------------------- ----------------------
1 2
2 4
3 3
Now, the problem is how can I find the I which has max J, by following I can find only what is the max J
SELECT MAX(j)
FROM testmax
but by following I get this error: ORA-00937: not a single-group group function:
SELECT i, MAX(j)
FROM testmax
Note that your question is still somewhat ambiguous; what should be returned when there is more than one record with a maximum value for J. Will you return one record or more than one? My answer is only applicable if you want one record returned.
And in that case, the query below, using FIRST/LAST aggregate function for i, is the most efficient query.
A small test with your table:
And the query using the LAST aggregate function (http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/functions076.htm#sthref1540):
Also with one table scan, but using an analytic function on all the rows, where a single aggregate will do just fine:
This one uses two table scans instead of one:
Regards,
Rob.