代码之家  ›  专栏  ›  技术社区  ›  noobie

oracle查询重复数据集中所有空值的最后一个已知值

  •  2
  • noobie  · 技术社区  · 6 年前

    我试图创建一个查询,用最后一个已知值(本例中为123)填充数据集中的所有空值。

    datetime          value
    01/SEP/18 00:30   111
    01/SEP/18 00:35   122
    01/SEP/18 00:40   101
    01/SEP/18 00:45   123
    01/SEP/18 00:50   NULL
    01/SEP/18 00:55   NULL
    01/SEP/18 13:00   NULL
    ...
    ...
    ...
    01/SEP/18 23:55   NULL
    

    datetime          value
    01/SEP/18 00:30   111
    01/SEP/18 00:35   122
    01/SEP/18 00:40   101
    01/SEP/18 00:45   123
    01/SEP/18 00:50   123
    01/SEP/18 00:55   123
    01/SEP/18 13:00   123
    ...
    ...
    ...
    01/SEP/18 23:55   123
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Tim Biegeleisen    6 年前

    UPDATE yourTable t1
    SET value = (SELECT value
                 FROM   (SELECT value, ROWNUM AS rn
                         FROM yourTable t2
                         WHERE t2.datetime < t1.datetime AND t2.value IS NOT NULL
                         ORDER BY datetime DESC)
                 WHERE  rn = 1)
    WHERE value IS NULL;
    

    value 哪一个不是 NULL .

    如果您只想选择当前表来生成预期的输出,可以尝试:

    SELECT
        datetime,
        CASE WHEN value IS NULL
             THEN (SELECT value
                   FROM   (SELECT value, ROWNUM AS rn
                           FROM yourTable t2
                           WHERE t2.datetime < t1.datetime AND t2.value IS NOT NULL
                           ORDER BY datetime DESC)
                   WHERE  rn = 1)
             ELSE value END AS value
    FROM yourTable t1;
    
        2
  •  0
  •   noobie    6 年前

    我通过以下查询获得了所需的结果:

      SELECT DATETIME, VALUE, last_value(VALUE) ignore nulls over (order by DATETIME) 
      FROM
      TABLE1;