postgres:获取每个组中前n个出现的值

postgres:获取每个组中前n个出现的值,第1张

postgres:获取每个组中前n个出现的值
with cte as (    select         t.user_id, t.letter,        row_number() over(partition by t.user_id order by count(*) desc) as row_num    from Table1 as t    group by t.user_id, t.letter)select    c.user_id,    max(case when c.row_num = 1 then c.letter end) as "1st-most-occurance",    max(case when c.row_num = 2 then c.letter end) as "2st-most-occurance"from cte as cwhere c.row_num <= 2group by c.user_id

= > SQL小提琴演示



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5673928.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存