获取最新的非空行

Get the most recent and not null row

我有以下 table:

id timestamp attribute-1 attribute-2
1 8/12 A
1 8/13 B
2 8/12 A
2 8/13 C B
2 8/14 B
3 8/12 B
3 8/14 C C

并且想为每个 id 创建一个具有最新属性的新 table,这意味着每个 id 的最新行,只要它同时具有 att-1 和 att-2,如果我不想从前一行中获取 att。应该是这样的:

id attribute-1 attribute-2
1 B A
2 B B
3 C C

您可以使用FIRST_VALUE() window函数:

SELECT DISTINCT id, 
       FIRST_VALUE(attribute1) OVER (PARTITION BY id ORDER BY attribute1 IS NULL, timestamp DESC) attribute1,
       FIRST_VALUE(attribute2) OVER (PARTITION BY id ORDER BY attribute2 IS NULL, timestamp DESC) attribute2
FROM tablename;

参见demo

我根据您输入的数据创建了一个 table table

CREATE TABLE data (
  "id" INTEGER,
  "timestamp" DATE,
  "attribute1" VARCHAR(1),
  "attribute2" VARCHAR(1)
);

INSERT INTO data
  ("id", "timestamp", "attribute1", "attribute2")
VALUES
  ('1', '2021-08-12', null, 'A'),
  ('1', '2021-08-13', 'B', null),
  ('2', '2021-08-12', null, 'A'),
  ('2', '2021-08-13', 'C', 'B'),
  ('2', '2021-08-14', 'B', null),
  ('3', '2021-08-12', 'B', null),
  ('3', '2021-08-14', 'C', 'C');

我认为您可以通过汇总数据并选择其中的第一个结果来实现您的结果:

SELECT 
    id, MAX(timestamp) AS timestamp_max,
    (array_remove(array_agg(attribute1 ORDER BY timestamp DESC), NULL))[1] AS attribute1_agg,
    (array_remove(array_agg(attribute2 ORDER BY timestamp DESC), NULL))[1] AS attribute1_agg
FROM data
GROUP BY id
ORDER BY id ASC;

给出了这个输出:

id timestamp_max attribute1_agg attribute1_agg
1 2021-08-13T00:00:00.000Z B A
2 2021-08-14T00:00:00.000Z B B
3 2021-08-14T00:00:00.000Z C C