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小提琴演示
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)