1

我有以下查询要旋转,如何在每个status最新updated_on行上过滤值obj_key

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 

表/数据在这里dbfiddle

我正在尝试,first_value(status) OVER( ORDER BY updated_on) AS status 但没有运气

我们可以根据每个 obj_key 的 max(updated_on) 获得状态吗?

4

3 回答 3

0

您的窗口功能缺少一个PARTITION BY。由于您需要每个所需的第一个值obj_key

first_value(status) OVER(PARTITION BY obj_key ORDER BY updated_on)
于 2021-06-08T07:23:04.597 回答
0

提供的 PKattributes(obj_key, type_key)您只需要添加row_number() obj_key 的所有 type_key。

select obj_key, max(updated_on) updated_on,
      max(case when rno = 1 then status end) as status, 
      max(array_to_string(v_date,'||'::text)) filter (where type_key=1) as "DOB",
      max(array_to_string(v_text,'||'::text)) filter (where type_key=2) as "First Name",
      max(array_to_string(v_text,'||'::text)) filter (where type_key=3) as "Last Name",
      max(array_to_string(v_number,'||'::text)) filter (where type_key=4) as "Contact"
from (
  select *, 
    row_number() over(partition by obj_key order by updated_on desc) rno
  from attributes) t
group by obj_key 

编辑 出于性能原因,您还可以使用这个旧的 pre-windows-functions 技巧

select obj_key, max(updated_on) updated_on,
      substring(max(updated_on::text || status), 20, 100) as status, 
      max(array_to_string(v_date,'||'::text)) filter (where type_key=1) as "DOB",
      max(array_to_string(v_text,'||'::text)) filter (where type_key=2) as "First Name",
      max(array_to_string(v_text,'||'::text)) filter (where type_key=3) as "Last Name",
      max(array_to_string(v_number,'||'::text)) filter (where type_key=4) as "Contact"
from  attributes t
group by obj_key;
于 2021-06-08T07:57:07.053 回答
0

试试这个版本,first_value(status) 更改为 last_value(status) 并且 OVER W 已移至聚合,因此在 COALESCE 语句中。

select DISTINCT ON (obj_key) 
   obj_key, updated_on, 
   last_value(status) OVER w AS created_by,
   COALESCE(max(array_to_string(v_date,'||'::text)) filter (where 
   att.type_key=1) OVER w ,(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) OVER w ,(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) OVER w ,(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) OVER w ,(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
   WINDOW w AS (PARTITION BY obj_key ORDER BY updated_on)
   ORDER  BY obj_key, updated_on DESC NULLS LAST;
于 2021-06-08T08:39:05.937 回答