使用优先级规则将多行合并为一

时间:2018-08-03 12:19:54

标签: sql postgresql

在Postgres中,我的表中有一个记录列表,我知道这些记录是重复的。鉴于此重复列表 (id IN (1,2,3,4)),我想使用以下优先级规则(从最高优先级到最低优先级)将它们合并为一条记录:

  • 非空值
  • 带有force=true的记录的值
  • 具有最新updated值的记录

例如:

enter image description here

应该变成这样:

enter image description here

Fiddle with schema here

1 个答案:

答案 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()做类似的事情。

编辑:

我没有意识到filterfirst_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。