SQL将多行中的字段的空值填充为先前的非空值

时间:2016-11-26 08:14:58

标签: sql postgresql ms-access netezza

我在netezza中有一个表(基于postgresql),如下所示。我需要创建一个新表,其中name的NULL值将替换为前一个非空行的name值。

表1

id name time    value
---------------------
1 john  11:00   324
2 NULL  12:00   645
3 NULL  13:00   324
4 bane  11:00   132
5 NULL  12:00   30
6 NULL  13:00   NULL
7 NULL  14:00   -1
8 zane  11:00   152
9 NULL  12:00   60
10 NULL 13:00   NULL

输出表

name    time    value
---------------------
john    11:00   324
john    12:00   645
john    13:00   324
bane    11:00   132
bane    12:00   30
bane    13:00   NULL
bane    14:00   -1
zane    11:00   152
zane    12:00   60
zane    13:00   NULL
笔记:
  1. 由于权限限制,无法更改table1,因此需要新表 就是这样。

  2. 需要在Netezza(最好)或MS Access中运行。

  3. 用于在Netezza中创建测试数据的代码如下所示。

    create temp table test (
        id int
        ,name varchar(10)
        ,time time
        ,value int
    )distribute on random;
    
    insert into test (id, name, time, value) values(1, 'joe', '10:00', 324);
    insert into test (id, name, time, value) values(2, null, '11:00', 645);
    insert into test (id, name, time, value) values(3, null, '12:00', 324);
    
    insert into test (id, name, time, value) values(4, 'bane', '10:00', 132);
    insert into test (id, name, time, value) values(5, null, '11:00', 30);
    insert into test (id, name, time, value) values(6, null, '12:00', null);
    insert into test (id, name, time, value) values(7, null, '13:00', -1);
    
    insert into test (id, name, time, value) values(8, 'zane', '10:00', 152);
    insert into test (id, name, time, value) values(9, null, '11:00', 60);
    insert into test (id, name, time, value) values(10, null, '12:00', null);
    

5 个答案:

答案 0 :(得分:1)

尝试这种递归的PostgreSQL查询:

WITH RECURSIVE t(id, name, time, value) AS (
  SELECT id, name, time, value FROM test WHERE id = (
    SELECT MIN(id) FROM test
  )
  UNION
  SELECT test.id, COALESCE(test.name, t.name), test.time, test.value
  FROM test, t WHERE test.id = (
    SELECT id FROM test WHERE id > t.id ORDER BY id LIMIT 1
  )
) SELECT * FROM t ORDER BY id;

请注意,这可能会在每一行上发出SELECT。如果您不想这样,可以使用视图解决方案:

CREATE VIEW test_view AS
SELECT id, LAG(id) OVER (ORDER BY id) lag_id, name, time, value FROM test;

WITH RECURSIVE t(id, name, time, value) AS (
  SELECT id, name, time, value FROM test_view WHERE lag_id IS NULL
  UNION ALL
  SELECT test_view.id, COALESCE(test_view.name, t.name),
    test_view.time, test_view.value
  FROM test_view, t WHERE test_view.lag_id = t.id
) SELECT * FROM t ORDER BY id;

这应该快得多。这个想法来自this article。 SQLFiddle:http://sqlfiddle.com/#!15/63f7b/1/1

答案 1 :(得分:1)

这适用于Access 2010:

SELECT 
    t1.id,
    (
        SELECT TOP 1 t2.name
        FROM test t2
        WHERE t2.id<=t1.id AND t2.name IS NOT NULL
        ORDER BY t2.id DESC
    ) AS name,
    t1.time,
    t1.value
FROM test t1

它也适用于其他SQL方言,尽管它们可能采用略有不同的方式TOP 1(例如,LIMIT 1是一种常见的变体)。

答案 2 :(得分:0)

您可以使用COALESCE函数和子查询实现此目的:

SELECT t.id, 
    COALESCE(t.name, (SELECT s.name FROM table s WHERE s.name IS NOT NULL AND s.id < t.id ORDER BY s.id LIMIT 1)) AS name,
    t.time,
    COALESCE(t.value, (SELECT s.value FROM table s WHERE s.value IS NOT NULL AND s.id < t.id ORDER BY s.id LIMIT 1)) AS value
FROM table t ORDER BY t.id

答案 3 :(得分:0)

基于MS Access的解决方案

SELECT (SELECT last(name)
          FROM test AS temp
          WHERE test.id >= temp.id AND temp.name IS NOT NULL) AS new_name, *
FROM test;

答案 4 :(得分:-2)

尝试使用引导功能。不确定它是否适用于postgre,对于Oracle来说也是如此。我认为这可以提供帮助。