0

我正在尝试获取列的最大值:

select * from
( select col1, count(*) as cnt from talbe1
group by col1
) dt
where cnt = max(cnt)

我试图获得确切的价值,它的工作方式如下:

where cnt = 5   

或者

where cnt > 3  

没关系,那么第一个查询有什么问题?

编辑:我放在那里的数字(5、3)是完全随机的,我想获得最大数量的cnt。

4

4 回答 4

2

聚合子句必须放在该HAVING部分中。但是,这不适用于您的查询。您可能想要做的是:

select top 1 col1, count(*) as cnt 
from talbe1
group by col1
order by count(*) desc
于 2012-11-28T11:09:16.737 回答
2

您可以使用HAVING子句来做到这一点。例如,如果您想获取cnt=3记录

Select col1, count(*) as cnt from talbe1
Group by col1
Having count(*)=3

如果你想得到MAX(cnt)

Select Top(1) col1, count(*) as cnt from talbe1
Group by col1
Order by cnt desc
于 2012-11-28T11:10:55.973 回答
1

我找到了一个解决方案,
这很简单:(我应该更加专注)

select max(cnt) from
( select Fld301, count(*) as cnt from TbC3
group by Fld301
) dt
于 2012-11-28T11:11:40.887 回答
1

这个查询怎么样:

select * from
(
    select 
        col1, 
        count(*) as cnt, 
        RANK() OVER(ORDER BY count(*) DESC) AS ran 
    from talbe1 
    group by col1
) dt
where ran=1
于 2012-11-28T11:22:26.407 回答