我如何改进此代码。
select code,max(total) from
(select code,count(*) from table_1 group by code)
上面的代码,因为我尝试从查询中对结果集执行MAX函数而无法正常工作,但是失败了。
答案 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);