gpt4 book ai didi

sql - 甲骨文 SQL : How to show empty weeks/weeks without data?

转载 作者:行者123 更新时间:2023-12-04 14:45:01 28 4
gpt4 key购买 nike

类似于this question ,但我的数据集有一个包含许多 ID 的附加列。每个 ID 都有一个可以追溯到固定时间范围的数据集,并且某些周可能会丢失数据 - 我想为缺失的周填写值。

例如,我想要这个:

ID      WEEKEND_DAY     VALUE
A00 2012-01-01 1
A00 2012-01-08 7
B00 2012-01-08 4
B00 2012-01-15 3

展开为:

ID      WEEKEND_DAY     VALUE
A00 2012-01-01 1
A00 2012-01-08 7
A00 2012-01-15 0
B00 2012-01-01 0
B00 2012-01-08 4
B00 2012-01-15 3

我已经有一个已知的 WEEKEND_DAY 范围(在上述情况下,每周从 2012-01-01 到 2012-01-15)我想填写。我该怎么做呢?

最佳答案

使用 partitioned outer join :

select data.id, weeks.weekend_day, nvl(value, 0) value
from
(
select date '2012-01-01' weekend_day from dual union all
select date '2012-01-08' weekend_day from dual union all
select date '2012-01-15' weekend_day from dual
) weeks
left join
(
select 'A00' id, date '2012-01-01' weekend_day, 1 value from dual union all
select 'A00' id, date '2012-01-08' weekend_day, 7 value from dual union all
select 'B00' id, date '2012-01-08' weekend_day, 4 value from dual union all
select 'B00' id, date '2012-01-15' weekend_day, 3 value from dual
) data
partition by (data.id)
on weeks.weekend_day = data.weekend_day

关于sql - 甲骨文 SQL : How to show empty weeks/weeks without data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11254121/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com