2013-04-24 52 views
9

我是Oracle和SQL世界的新手。我有一個問題,我無法弄清楚我的生活,我花了幾個小時嘗試不同的方法,我無法得到我期望的結果。所以繼承我的查詢:如何在子查詢結果上使用MAX()?

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) 
    ) g 
WHERE g.membership_count = (select MAX(membership_count) from g); 

因此,內部查詢完美工作,並返回兩個結果。現在我有這兩個值,我想弄清楚如何返回最大的membership_count值,這是我一直在卡住的行。在上面的查詢中,我嘗試在where子句中使用MAX(),但在裏面選擇我不斷收到錯誤'table not found'(意思是'g')。所以我的問題是如何對我的子查詢的結果使用MAX()函數?任何想法或建議將不勝感激!!!!!

+0

如果您不知道,[sqlfiddle](http://www.sqlfiddle.com)確實有Oracle,並且您可以使用各種格式的語句。 – hd1 2013-04-24 03:12:43

回答

0

你可以嘗試像

SELECT membership.mem_desc,membership.mem_max_rentals,membership_history.mem_type, membership_count, rank() over ORDER BY membership_count DESC as ranky 
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) 
) 
WHERE ranky =1; 
10

你並不需要一個發現最大值的子查詢。
相反,你只需要第一行有下令行後:

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 
0

您不能直接where子句中使用派生表,它會給table or view does not exist錯誤 所以,讓你可以使用HAVING最大計數值條款或Analytical FunctionsRownum

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; 
3

這是所有好的SQL。

找到一個列值的最佳方式使用最大主鍵列值是:

SELECT .... from tablename 
WHERE ... AND 
    (SELECT max(primary key name) FROM tablename WHERE ....) = primary_key_name 

這個例子會返回一個標量值。

0

我認爲最乾淨的解決方案是使用ALL比較條件。它用於將值與列表或子查詢進行比較。

SELECT 
    m.mem_desc, 
    m.mem_max_rentals, 
    mh.mem_type,  
    COUNT(mh.mem_type) as membership_count 
FROM membership_history mh 
JOIN membership m ON m.mem_type = mh.mem_type 
GROUP BY mh.mem_type,m.mem_desc,m.mem_max_rentals 
HAVING membership_count >= ALL (
    SELECT count(*) 
    FROM membership_history 
    GROUP BY mem_type 
)