我有一张桌子(最终可能会使用Postgres)
这是输入的样子。
我想根据相同的标准将产品放入最终分组。 例如,prod-A,prod-B,prod-X都属于同一组,因为它们的标准行是相同的。 (不能部分匹配)。
因此我希望输出是这样的
答案 0 :(得分:2)
这回答了标记邮戳的问题的原始版本。
我认为最简单的方法是聚合数据,然后分配组:
select product, dense_rank() over (order by criterias) as grp
from (select product, array_agg(criteria order by criteria) as criterias
from t
group by product
) p;
如果您需要原始数据,可以将其加入:
select t.*, tt.grp
from t join
(select product, dense_rank() over (order by criterias) as grp
from (select product, array_agg(criteria order by criteria) as criterias
from t
group by product
) p
) tt
on t.product = tt.product;
你甚至不需要额外的子查询:
select t.*, tt.grp
from t join
(select product, dense_rank() over (order by criterias) as grp
from (select product,
dense_rank() over (order by array_agg(criteria order by criteria)) as criterias
from t
group by product
) p
) tt
on t.product = tt.product;