How to use MAX() on a subquery result?

后端 未结 7 1829
名媛妹妹
名媛妹妹 2021-02-07 08:48

I am new to Oracle and the SQL world. I have a slight issue with a query that I cannot figure out for the life of me, I have spent a few hours trying different approaches and I

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-07 09:30

    You don't need the subquery that finds the maximum value.
    Instead, ; you just need the first row after having ordered the rows:

    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
    

提交回复
热议问题