- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有下表:
ID height
personA 182
personA 182
personA 182
personA 192
personA 172
personB 175
personB 175
我想提取此人最常出现的高度,因为我怀疑 192 是一个拼写错误。到目前为止,我有:
select ID, height, count(ID,height) as cnt
from tbl
group by ID, height
having max(cnt);
我想要的输出是:
ID height
personA 182
personB 175
最佳答案
您可以简单地使用为您的用例设计的模式
。请注意,这不会处理关系
select id, mode(height) as height
from t
group by id;
另一种不使用分析函数
的替代方法也能处理关系
with cte as
(select id, height, count(*) as cnt
from t
group by id, height)
select id, height
from cte
where (id, cnt) in (select id, max(cnt)
from cte
group by id)
如果您要使用 Lukasz 的回答中巧妙使用的 qualify
子句来实现上述内容,您可以这样做
select id, height
from t
group by id, height
qualify max( count(*) ) over (partition by id) = count(*)
关于sql - 按组提取最常见(计数最高)的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70925880/
我是一名优秀的程序员,十分优秀!