我正试图
- 为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_
1条答案
按热度按时间njthzxwz1#
您可以找到所有的日期,然后使用
PARTITION
edOUTER JOIN
将其连接到表中,然后使用LAST_VALUE
分析函数:其中,对于样本数据:
输出:
| 产品名称_|日期_|参数_|值|
| --|--|--|--|
| 一|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