oracle 最后一个已知值或0,否则为NULL

mrwjdhj3  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(100)

我正试图

  • 为date_和name_的每个组合获取行**- param_ tuple**
  • 并重复最后一个已知值将NULL转换为0如果是全新的行(即,不存在date_、name_、param_的组合)
    输入
NAME_   DATE_   PARAM_  VALUE
A   16-09-2023  A1      NULL
A   16-09-2023  A2      11
A   17-09-2023  A2      10
A   17-09-2023  A3      0
A   18-09-2023  A1      12
B   16-09-2023  B1      2
B   18-09-2023  B1      NULL
B   18-09-2023  B2      4

预期

NAME_   DATE_   PARAM   VALUE
A   16-09-2023  A1      0
A   16-09-2023  A2      11
A   16-09-2023  A3      NULL
A   17-09-2023  A1      10
A   17-09-2023  A2      10
A   17-09-2023  A3      0
A   18-09-2023  A1      12
A   18-09-2023  A2      10
A   18-09-2023  A3      0
B   16-09-2023  B1      2
B   16-09-2023  B2      NULL
B   17-09-2023  B1      2
B   17-09-2023  B2      NULL
B   18-09-2023  B1      0
B   18-09-2023  B2      4

尝试

WITH Dates AS (
  SELECT DISTINCT DATE_ AS date_value
  FROM test
),
NameParam AS (
  SELECT DISTINCT NAME_, PARAM_
  FROM test
)
SELECT
  npc.NAME_,
  d.date_value AS DATE_,
  npc.PARAM_,
  NVL(yt.VALUE_, 0) as VALUE_
FROM Dates d
CROSS JOIN NameParam npc
LEFT JOIN test yt
  ON d.date_value = yt.DATE_
  AND npc.NAME_ = yt.NAME_
  AND npc.PARAM_ = yt.PARAM_
ORDER BY npc.NAME_, d.date_value, npc.PARAM_

fiddle

njthzxwz

njthzxwz1#

您可以找到所有的日期,然后使用PARTITION ed OUTER JOIN将其连接到表中,然后使用LAST_VALUE分析函数:

SELECT  t.name_,
        d.date_,
        t.param_,
        LAST_VALUE(t.value) IGNORE NULLS
          OVER (PARTITION BY t.name_, t.param_ ORDER BY d.date_)
          AS value
FROM    (SELECT DISTINCT date_ FROM table_name) d
        LEFT OUTER JOIN (
          SELECT name_,
                 date_,
                 param_,
                 COALESCE(value, 0) AS value
          FROM   table_name
        ) t
        PARTITION BY (name_, param_)
        ON (t.date_ = d.date_)
ORDER BY name_, date_, param_

其中,对于样本数据:

CREATE TABLE table_name (NAME_, DATE_, PARAM_, VALUE) AS
  SELECT 'A', DATE '2023-09-16', 'A1', NULL FROM DUAL UNION ALL
  SELECT 'A', DATE '2023-09-16', 'A2',   11 FROM DUAL UNION ALL
  SELECT 'A', DATE '2023-09-17', 'A2',   10 FROM DUAL UNION ALL
  SELECT 'A', DATE '2023-09-17', 'A3',    0 FROM DUAL UNION ALL
  SELECT 'A', DATE '2023-09-18', 'A1',   12 FROM DUAL UNION ALL
  SELECT 'B', DATE '2023-09-16', 'B1',    2 FROM DUAL UNION ALL
  SELECT 'B', DATE '2023-09-18', 'B1', NULL FROM DUAL UNION ALL
  SELECT 'B', DATE '2023-09-18', 'B2',    4 FROM DUAL;

输出:
| 产品名称_|日期_|参数_|值|
| --|--|--|--|
| 一|2023-09-16 00:00:00| A1| 0 |
| 一|2023-09-16 00:00:00| A2| 11 |
| 一|2023-09-16 00:00:00| A3| * 空 *|
| 一|2023-09-17 00:00:00| A1| 0 |
| 一|2023-09-17 00:00:00| A2| 10 |
| 一|2023-09-17 00:00:00| A3| 0 |
| 一|2023-09-18 00:00:00| A1| 12 |
| 一|2023-09-18 00:00:00| A2| 10 |
| 一|2023-09-18 00:00:00| A3| 0 |
| B| 2023-09-16 00:00:00| B1| 2 |
| B| 2023-09-16 00:00:00| B2| * 空 *|
| B| 2023-09-17 00:00:00| B1| 2 |
| B| 2023-09-17 00:00:00| B2| * 空 *|
| B| 2023-09-18 00:00:00| B1| 0 |
| B| 2023-09-18 00:00:00| B2| 4 |
fiddle

相关问题