SELECT MAX of COUNT [英] SELECT MAX of COUNT
本文介绍了SELECT MAX of COUNT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张很好"的桌子.它包含一列 app_rate_unit(类型:nvarchar).我的目标是计算表中每个不同的值,并让 DBMS (MS Server 2005) 给我出现次数最多的值.
I have a table "well". It contains a column app_rate_unit (type: nvarchar). My goal is to count every distinct value in the table and let the DBMS (MS Server 2005) give me the most occurring one.
这是我的代码:
SELECT MAX(app_rate_unit) AS MAX_APP
FROM (SELECT app_rate_unit, COUNT(*) AS co
FROM dbo.well AS w
GROUP BY app_rate_unit
) AS derivedtbl_1
然而,问题在于,我的 DBMS 实际上为我提供了最低的计数.
The poblem with it is however, that my DBMS actually delivers the lowest count to me.
SideQuestion:如何在计数时过滤外键(在表中)和 NOT NULL(在 app_rate_unit 中)?
SideQuestion: How do I filter for a foreign key (in the table) and NOT NULL (in app_rate_unit) when counting?
推荐答案
select top 1 app_rate_unit, count(*) from dbo.well
group by app_rate_unit
order by count(*) desc
这篇关于SELECT MAX of COUNT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文