1

I am trying to run the following query on an Oracle express edition database.

SELECT SIGHTING_ID, MAX(DISTANCE)
FROM
(
    SELECT SIGHTING_ID,
          SQRT(POWER(LATITUDE + 28, 2) + POWER(LONGITUDE - 151, 2)) AS DISTANCE
    FROM SIGHTINGS
)
GROUP BY SIGHTING_ID;

The purpose of the query is to return The SIGHTING_ID and DISTANCE for the sighting with the maximum distance from the co-ords (-28, 151)

The description for the table SIGHTINGS is as follows

 desc SIGHTINGS
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 SIGHTING_ID                               NOT NULL NUMBER
 SPOTTER_ID                                         NUMBER
 BIRD_ID                                            NUMBER
 LATITUDE                                           NUMBER
 LONGITUDE                                          NUMBER
 SIGHTING_DATE                                      DATE
 DESCRIPTION                                        VARCHAR2(255)

When I run the query instead of selecting the maximum distance it selects and shows the entire list of sightings and their respective distances. SIGHTINGS has a large amount of tuples but below is a small list of the current output

 SIGHTING_ID MAX(DISTANCE)
 ----------- -------------
 264172    2.01556444
 264174    2.34029913
 264180    2.87647354
 264198    .637887137
 264205    2.08568933
 264211    .232594067
 264215    2.34104677
 264221           .75
 264224    .148660687
 264235    .684470598

My question is how do I make it so that the MAX aggregate function works so that it returns just the maximum distance and the respect SIGHTING_ID?

3 Answers 3

2

Use Oracle's rownum :

SELECT * FROM 
    (SELECT SIGHTING_ID, SQRT(POWER(LATITUDE + 28, 2) + POWER(LONGITUDE - 151, 2)) AS DISTANCE
     FROM SIGHTINGS
     ORDER BY distance DESC)
WHERE ROWNUM = 1

The reason you're getting a record for each SIGHTING_ID is because you are grouping by it. A group by clause means -> give me 1 record for each combination of the columns mentioned inside the clause .

Sign up to request clarification or add additional context in comments.

14 Comments

If there are two records with the maximum distance in that case it would skip one of the records.
And how do you know that's not what he wants? @hemalp108
I've no doubt in your answer, let him decide what he wants. I am just asking it for my curiosity.
Thank you so much, worked like a charm after removing the extra bracket
@FrankSchmitt - You are right again. Not enough coffee yet!
|
1

In Oracle 12c+, you can use ANSI standard functionality:

SELECT SIGHTING_ID,
      SQRT(POWER(LATITUDE + 28, 2) + POWER(LONGITUDE - 151, 2)) AS DISTANCE
FROM SIGHTINGS
ORDER BY distance DESC
FETCH FIRST 1 ROW ONLY;

In earlier versions, @sagi's solution is probably the best way.

4 Comments

The correct solution for this kind of problems is the first/last aggregate function (as I demonstrate in my answer). docs.oracle.com/cd/B19306_01/server.102/b14200/functions056.htm
@mathguy Your wording is misleading. Your approach is one of several correct solutions - Gordon Linoff's is another one, and sagi's yet another.
@FrankSchmitt - You are right, I should have said "best", not "correct." For Oracle 12c, Gordon's solution is equally good; I was disagreeing with his assessment for earlier versions. I would edit my comments but on SO that is not possible.
@mathguy . . . I prefer Sagi's solution, because it more easily generalizes to multiple columns (or all columns within a row).
0
select min(sighting_id) keep (dense_rank first order by distance) as sighting_id,
       min(distance)                                              as distance
from sightings;

This will find the row or rows with the shortest distance, and from those rows (and those only), if there are ties (the shortest distance is achieved by two or more sighting id's), it selects the one with the smallest sighting_id. If instead you want the one with the greatest latitude (say), still from those with the shortest distance, change order by distance to order by distance, latitude desc. Other combinations are treated the same way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.