在Postgres中,我的表中有一个记录列表,我知道这些记录是重复的。鉴于此重复列表
(id IN (1,2,3,4)
),我想使用以下优先级规则(从最高优先级到最低优先级)将它们合并为一条记录:
force=true
的记录的值updated
值的记录例如:
应该变成这样:
答案 0 :(得分:3)
我认为您需要这样的东西:
select distinct
first_value(col1) filter (where col1 is not null) over (order by force desc, updated desc) as col1,
first_value(col2) filter (where col2 is not null) over (order by force desc, updated desc) as col2,
first_value(col3) filter (where col3 is not null) over (order by force desc, updated desc) as col3,
first_value(col4) filter (where col4 is not null) over (order by force desc, updated desc) as col4
from t
where id in (1, 2, 3, 4);
我不喜欢带有窗口功能的select distinct
,但Postgres尚未提供first_value()
作为聚合功能。
您也可以使用array_agg()
做类似的事情。
编辑:
我没有意识到filter
与first_value()
不兼容。啊array_agg()
的格式为:
select (array_agg(column1 order by force desc, updated desc) filter (where column1 is not null))[1] as column1,
(array_agg(column2 order by force desc, updated desc) filter (where column2 is not null))[1] as column2,
(array_agg(column3 order by force desc, updated desc) filter (where column3 is not null))[1] as column3
from test_table;
Here是SQL Fiddle。