17,377
社区成员
发帖
与我相关
我的任务
分享
with test as
(select 'c' a,'e' b from dual
union all select 'c' a,'e' b from dual
union all select 'c' a,'e' b from dual
union all select 'c' a,'f' b from dual
union all select 'c' a,'f' b from dual
union all select 'd' a,'g' b from dual
union all select 'd' a,'g' b from dual
union all select 'd' a,'g' b from dual
union all select 'd' a,'h' b from dual)
select a,b,c
from
(select a,b,c,rank() over(partition by a order by c desc ) rn
from
(select a,b,count(b) c from test group by a,b))
where rn=1;
SELECT a, b, cnt
FROM (SELECT a, b, cnt, row_number() over(PARTITION BY a ORDER BY cnt DESC) rn
FROM (SELECT a, b, COUNT(b) over(PARTITION BY a, b) cnt FROM x))
WHERE rn = 1;
with a as (
select '1' fxh,'c' fa, 'e' fb from dual
union
select '2' fxh,'c' fa, 'e' fb from dual
union
select '3' fxh,'c' fa, 'e' fb from dual
union
select '4' fxh,'c' fa, 'f' fb from dual
union
select '5' fxh,'c' fa, 'f' fb from dual
union
select '6' fxh,'d' fa, 'g' fb from dual
union
select '7' fxh,'d' fa, 'g' fb from dual
union
select '8' fxh,'d' fa, 'g' fb from dual
union
select '9' fxh,'d' fa, 'h' fb from dual
)
SELECT b.fa as "第一列",b.fb as "出现最多的",ct as "出现的次数" FROM (
SELECT max(ct)over(partition by fa ) maxct ,a.* FROM (
SELECT count(fb)over(partition by fa ,fb ) ct,fa,fb,row_number()over(partition by fa ,fb order by fa )rn ,fxh
FROM a
) a where a.rn=1
) b where maxct = ct
最近脑子有点抽,写的可能有些复杂,但是可以完成你的需求,也希望有大腿写出简单的sql,学习一下