2011-11-25 92 views
0

我該如何改進這段代碼。oracle sql如何取回1行只返回結果

select code,max(total) from 
(select code,count(*) from table_1 group by code) 

上面的代碼,不工作,因爲我試圖從查詢結果集上做MAX函數,但失敗。

+0

查看http:/ /stackoverflow.com/questions/470542/how-do-i-limit-the-number-of-rows-returned-by-an-oracle-query和http://www.club-oracle.com/forums/limit -clause-for-oracle-sql-t637/ –

+0

目標是什麼? 'total'是'table_1'中的一列還是'count(*)'的值? – Yahia

回答

2

如果你只是想號碼,然後你可以使用這個:

select max(total) 
from (
    select code, 
      count(*) as total -- you forgot the column alias here 
    from table_1 
    group by code 
) 

如果你想要的代碼數,使用以下命令:

with count_result as (
    select code, 
      count(*) as total 
    from table_1 
    group by code 
) 
select code, 
     total 
from count_result 
where total = (select max(total) from count_result);