添加缺少的日期行继承以前的值
Add missing date rows inheriting the previous value
我正在尝试使用 Big Query 获取“期望结果”上的 table(见下图),从一个 Dummy table 开始,它可以按如下方式初始化:
with dummy_data as
(
select * from unnest([
struct(
date('2021-06-10') AS timestamp,
'A' AS category,
'5' as value),
(date('2021-06-11'), 'B', '3'),
(date('2021-06-12'), 'C', '2'),
(date('2021-06-14'), 'A', '7')
])
)
select * from dummy_data
我从生成时间序列和交叉连接开始,但无法摆脱由此产生的不必要的行:
with dummy_data as
(
select * from unnest([
struct(
date('2021-06-10') AS timestamp,
'A' AS category,
'5' as values ),
(date('2021-06-11'), 'B', '3'),
(date('2021-06-12'), 'C', '2'),
(date('2021-06-14'), 'A', '7')
])
),
-- Initialize date series that covers needed range
date_series as (
select
*
from
unnest(generate_date_array('2021-06-10', '2021-06-14', interval 1 day)) as date
),
-- cross_join
cross_join as (
select * from dummy_data
cross join date_series
)
select * from cross_join
结果如下(蓝色行是我需要的新行,灰色行是我想删除的行):
谢谢!
考虑以下方法
select timestamp, category, value
from (
select timestamp as start_timestamp, category, value, ifnull(
lead(timestamp) over(partition by category order by timestamp) - 1,
max(timestamp) over(order by timestamp desc)
) next_timestamp
from dummy_data
), unnest(generate_date_array(start_timestamp, next_timestamp)) timestamp
如果应用于您问题中的示例数据 - 输出为
我正在尝试使用 Big Query 获取“期望结果”上的 table(见下图),从一个 Dummy table 开始,它可以按如下方式初始化:
with dummy_data as
(
select * from unnest([
struct(
date('2021-06-10') AS timestamp,
'A' AS category,
'5' as value),
(date('2021-06-11'), 'B', '3'),
(date('2021-06-12'), 'C', '2'),
(date('2021-06-14'), 'A', '7')
])
)
select * from dummy_data
我从生成时间序列和交叉连接开始,但无法摆脱由此产生的不必要的行:
with dummy_data as
(
select * from unnest([
struct(
date('2021-06-10') AS timestamp,
'A' AS category,
'5' as values ),
(date('2021-06-11'), 'B', '3'),
(date('2021-06-12'), 'C', '2'),
(date('2021-06-14'), 'A', '7')
])
),
-- Initialize date series that covers needed range
date_series as (
select
*
from
unnest(generate_date_array('2021-06-10', '2021-06-14', interval 1 day)) as date
),
-- cross_join
cross_join as (
select * from dummy_data
cross join date_series
)
select * from cross_join
结果如下(蓝色行是我需要的新行,灰色行是我想删除的行):
谢谢!
考虑以下方法
select timestamp, category, value
from (
select timestamp as start_timestamp, category, value, ifnull(
lead(timestamp) over(partition by category order by timestamp) - 1,
max(timestamp) over(order by timestamp desc)
) next_timestamp
from dummy_data
), unnest(generate_date_array(start_timestamp, next_timestamp)) timestamp
如果应用于您问题中的示例数据 - 输出为