2011-11-25 97 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);