您不能直接在 where 子句中使用派生表,它会给出table or view does not exist
错误,因此要获得可以使用HAVING
子句或Analytical Functions
之Rownum
类的最大计数值
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 a
JOIN membership b ON b.mem_type = a.mem_type
group by (membership_history.mem_type,membership.mem_desc,membership.mem_max_rentals)
having count(a.MEM_TYPE) = (Select
MAX(count(a.MEM_TYPE)) from membership_history a
JOIN membership b ON b.mem_type = a.mem_type
group by (a.mem_type,b.mem_desc,b.mem_max_rentals)));
(或者)
select * from
(SELECT g.*,rank() over (order by membership_count desc) rnk 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)) g)
WHERE rnk=1;
(或者)
select * from
(SELECT g.*,rownum rn 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 membership_count desc) g)
WHERE rn=1;