如何用前一行的值填充空列?
Posted
技术标签:
【中文标题】如何用前一行的值填充空列?【英文标题】:How do I fill null columns with values from the previous row? 【发布时间】:2019-11-16 23:02:23 【问题描述】:我正在尝试在这里执行一些操作。我想让所有的列都充满值。但是当我有空列时,我想用前一个非空列中的值填充它。
with cte as (
select '2019-11-12 16:01:55' as timestamp, null as owner_id, null as owner_assigneddate, null as lastmodifieddate union all
select '2019-11-12 19:03:18' as timestamp, 39530934 as owner_id, '2019-11-12 19:03:18' as owner_assigneddate, '2019-11-12 19:03:18' as lastmodifieddate union all
select '2019-11-12 19:03:19' as timestamp, null as owner_id, null as owner_assigneddate, '2019-11-12 19:03:19' as lastmodifieddate union all
select '2019-11-12 19:03:20' as timestamp, null as owner_id, null as owner_assigneddate, '2019-11-12 19:03:20' as lastmodifieddate union all
select '2019-11-12 19:03:31' as timestamp, 40320368 as owner_id, '2019-11-12 19:03:31' as owner_assigneddate, '2019-11-12 19:03:31' as lastmodifieddate union all
select '2019-11-12 19:03:33' as timestamp, null as owner_id, null as owner_assigneddate, '2019-11-12 19:03:33' as lastmodifieddate union all
select '2019-11-12 19:03:56' as timestamp, null as owner_id, null as owner_assigneddate, '2019-11-12 19:03:356' as lastmodifieddate)
select timestamp,
owner_id,
owner_assigneddate,
lastmodifieddate,
COALESCE(owner_id, LEAD(owner_id) OVER(ORDER BY timestamp DESC)) AS test_column
from cte order by timestamp asc
通过上一个查询,我已经设法将值仅放在下一行。
我想要做的是让所有列都填充基于前一行的值。 第 4 行的值应为 39530934,第 7 行的值应为 40320368。 我想我在这里遗漏了一些东西,但我不知道是什么。
【问题讨论】:
【参考方案1】:在 BigQuery 中使用 LAST_VALUE()
和 IGNORE NULLS
选项和 COALESCE()
:
select timestamp,
COALESCE(owner_id, last_value(owner_id ignore nulls) over (order by timestamp)) as owner_id,
COALESCE(owner_assigneddate, LAST_VALUE(owner_assigneddate IGNORE NULLS) OVER (ORDER BY TIMESTAMP)) as owner_assigneddate,
COALESCE(lastmodifieddate, LAST_VALUE(lastmodifieddate IGNORE NULLS) OVER (ORDER BY TIMESTAMP)) as lastmodifieddate
from cte order by timestamp asc
【讨论】:
【参考方案2】:就问题而言,Big Query 不支持窗口函数中的ignore null
。这是一个解决方案,它依赖于窗口最大值来定位保存最后一个非空owner_id
的记录(这假设时间戳的唯一性)。有了这些信息,您就可以引入相应的 owner_id
并加入。
select
c.timestamp,
coalesce(c.owner_id, c_lag.owner_id) owner_id,
c.owner_assigneddate,
c.lastmodifieddate
from
(
select
cte.*,
max(case when owner_id is not null then timestamp end)
over(order by timestamp rows unbounded preceding) target_timestamp
from cte
) c
left join cte c_lag
on c.owner_id is null
and c_lag.timestamp = c.target_timestamp
Demo on DB Fiddle:
时间戳 |所有者 ID |所有者分配日期 |最后修改日期 :----------------- | --------: | :----------------- | :-------------------- 2019-11-12 16:01:55 | 空 | 空 | 空 2019-11-12 19:03:18 | 39530934 | 2019-11-12 19:03:18 | 2019-11-12 19:03:18 2019-11-12 19:03:19 | 39530934 | 空 | 2019-11-12 19:03:19 2019-11-12 19:03:20 | 39530934 | 空 | 2019-11-12 19:03:20 2019-11-12 19:03:31 | 40320368 | 2019-11-12 19:03:31 | 2019-11-12 19:03:31 2019-11-12 19:03:33 | 40320368 | 空 | 2019-11-12 19:03:33 2019-11-12 19:03:56 | 40320368 | 空 | 2019-11-12 19:03:356注意:如果需要,为了更好地理解逻辑,您可以独立运行内部查询以查看它返回的内容(请参阅 db fiddle)。
编辑
重读本文,我发现window max提供的信息已经在您的原始数据中,在owner_assigneddate
列中......所以这要简单得多:
select
c.timestamp,
coalesce(c.owner_id, c_lag.owner_id) owner_id,
c.owner_assigneddate,
c.lastmodifieddate
from
cte c
left join cte c_lag
on c.owner_id is null
and c_lag.timestamp = c.owner_assigneddate
【讨论】:
【参考方案3】:这应该适用于您的 cte
定义:
...
select timestamp,
owner_id,
owner_assigneddate,
lastmodifieddate,
LAST_VALUE(owner_id IGNORE NULLS)
OVER(ORDER BY timestamp ASC ROWS BETWEEN
UNBOUNDED PRECEDING AND CURRENT ROW) AS test_column
from cte order by timestamp asc
【讨论】:
以上是关于如何用前一行的值填充空列?的主要内容,如果未能解决你的问题,请参考以下文章