我正在寻找最简单的方法来返回分组的 select 语句的多列结果中最常见的值。我在网上找到的所有内容都指向选择中单个项目的排名,或者在 GROUP BY 之外单独处理每一列。
样本数据:
SELECT 100 as "auser",
'A' as "instance1", 'M' as "instance2"
union all select 100, 'B', 'M'
union all select 100,'C', 'N'
union all select 100, 'B', 'O'
union all select 200,'D', 'P'
union all select 200, 'E', 'P'
union all select 200,'F', 'P'
union all select 200, 'F', 'Q'
样本数据结果:
auser instance1 instance2
100 A M
100 B M
100 C N
100 B O
200 D P
200 E P
200 F P
200 F Q
查询逻辑(我如何在脑海中看到它):
SELECT auser, most_common(instance1), most_common(instance2)
FROM datasample
GROUP BY auser;
期望的结果:
100 B M
200 F P