我们说我有下表
Sku | Number | Name
11 1 hat
12 1 hat
13 1 hats
22 2 car
33 3 truck
44 4 boat
45 4 boat
是否有一种简单的方法可以找出如何找到每个数字中的差异。例如,使用上表,我希望查询输出:
13 | 1 | hats
原因是因为只要数字与名称匹配,我们的程序就会处理行。如果存在名称不匹配但其余名称不匹配的实例,则会失败。
答案 0 :(得分:4)
您可以使用窗口函数和聚合找到最常见的值("模式"):
select t.*
from (select number, name, count(*) as cnt,
row_number() over (partition by number order by count(*) desc) as seqnum
from t
group by number, name
) t
where seqnum = 1;
然后,您可以使用join
查找不是模式的所有内容。更简单的方法就是更改where
条件:
select t.*
from (select number, name, count(*) as cnt,
row_number() over (partition by number order by count(*) desc) as seqnum
from t
group by number, name
) t
where seqnum > 1;
注意:如果最常见值的频率存在联系,则选择任意最常见的值。
编辑:
实际上,如果你想要原始的skus,你也可以加入:
with modes as (
select t.*
from (select number, name, count(*) as cnt,
row_number() over (partition by number order by count(*) desc) as seqnum
from t
group by number, name
) t
where seqnum = 1
)
select t.*
from t join
modes
on t.number = modes.number and t.name <> modes.name;
这将忽略NULL
值(但可以轻松修复逻辑以适应它们)。