I keep getting this error when I tried to execute this query, although I couldn’t figure out what went wrong. I’m using Oracle and JDBC.
Here’s the query:
SELECT Temp.flight_number, Temp.avgprice
FROM (SELECT P.flight_number, AVG (P.amount) AS avgprice
FROM purchase P
GROUP BY P.flight_number) AS Temp
WHERE Temp.avgprice = (SELECT MAX (Temp.avgprice)
FROM Temp)
I’m trying to get the maximum of average price of the tickets that customers have booked, group by flight_number.
A few issues.
ASin Oracle to alias a table nameTEMPin the subquery like that.Using analytic functions is generally going to be the most efficient approach.
You could also do something like this with subquery factoring