pgsql:如何将 Array_to_string 放入 COALESCE

pgsql: How to put Array_to_string inside the COALESCE

在 select 语句中我有列 max(array_to_string(v_date,'||'::text)) filter (where type_key=1) .

现在我必须使用 COALESCE 来显示 v_date 个空值的默认值。

Tables/data 在这里 dbfiddle

select obj_key, max(updated_on) as updated_on, max(att.status) as status, 
      COALESCE(max(array_to_string(v_date,'||'::text)) filter (where att.type_key=1),max(default_value))  as "DOB",
      max(array_to_string(v_text,'||'::text)) filter (where att.type_key=2) as "First Name",
      max(array_to_string(v_text,'||'::text)) filter (where att.type_key=3) as "Last Name",
      max(array_to_string(v_number,'||'::text)) filter (where att.type_key=4) as "Contact"
from attributes att right join types ty on att.type_key=ty.type_key
group by obj_key 

obj_key 3 的出生日期必须是“1/1/1950”,但它给出的样本 1 ?

是否可以在 coalesce 中使用 array_to_string 来获得 none 空值(默认值)?

您需要再次加入类型 table 以获得值,因为当您加入按 obj_key.

分组的属性 table

试试这个版本:

select obj_key, max(updated_on) as updated_on, max(att.status) as status, 
  COALESCE(max(array_to_string(v_date,'||'::text)) filter (where att.type_key=1),(select default_value from types where type_key=1))  as "DOB",
  COALESCE(max(array_to_string(v_text,'||'::text)) filter (where att.type_key=2),(select default_value from types where type_key=2)) as "First Name",
  COALESCE(max(array_to_string(v_text,'||'::text)) filter (where att.type_key=3),(select default_value from types where type_key=3))as "Last Name",
  COALESCE(max(array_to_string(v_number,'||'::text)) filter (where att.type_key=4),(select default_value from types where type_key=4)) as "Contact"
  from attributes att right join types ty on att.type_key=ty.type_key
  group by obj_key