oracle sql如何仅从返回结果中获取1行

时间:2011-11-25 08:15:34

标签: sql oracle

我如何改进此代码。

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

上面的代码,因为我尝试从查询中对结果集执行MAX函数而无法正常工作,但是失败了。

1 个答案:

答案 0 :(得分: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);