Oracle
 sql >> Database >  >> RDS >> Oracle

Come utilizzare MAX() su un risultato di sottoquery?

Non è necessaria la sottoquery che trova il valore massimo.
Invece,; hai solo bisogno del primo riga dopo aver ordinato le righe:

select * from (
  select 
    membership.mem_desc,
    membership.mem_max_rentals,
    membership_history.mem_type,      
    count(membership_history.MEM_TYPE) as membership_count
  from membership_history
  JOIN membership ON membership.mem_type = membership_history.mem_type
  group by (membership_history.mem_type,membership.mem_desc,membership.mem_max_rentals)
  ORDER BY 4 DESC  -- Added this line
) g
WHERE ROWNUM = 1. -- Added this line