您不能直接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)));
(OR)
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;
(OR)
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;
如果您不知道,[sqlfiddle](http://www.sqlfiddle.com)確實有Oracle,並且您可以使用各種格式的語句。 – hd1 2013-04-24 03:12:43