Oracle Sql获取对于另一列的每个不同值出现的值最多的值

所以我要说我有这张桌子

language | offer
chinese  |   1
chinese  |   1
english  |   1
spanish  |   2
spanish  |   2
italian  |   2
french   |   3

而且我希望每个不同的报价出现的languange就像这样

language | offer
chinese  |   1
spanish  |   2
french   |   3

我如何在oracle sql中执行此操作?

最佳答案 这是使用公用表表达式执行此操作的一种方法.

SQL Fiddle

在第一个cte中,您可以计算按要约和语言分组的计数.
 在下一个cte中,使用rank或row_number为具有最高语言计数的商品分配1.
 最后,从排名第一的行中进行选择.

with counts as(
select offer, language, count(*) cnt
from tablename
group by offer, language)
,ranking as
(select rank() over(partition by offer order by cnt desc) rnk
, c.*
from counts c)
select language, offer
from ranking 
where rnk = 1

没有窗口函数的替代方法:

with counts as (
select offer, language, count(*) cnt
from tablename
group by offer, language)
,maxcount as (select offer, max(cnt) mxcnt from counts group by offer)
select c.language, m.offer
from counts c
join maxcount m on m.offer = c.offer and m.mxcnt = c.cnt
点赞