Sql 如何用前一行中的值填充空列?
我想在这里表演一些东西。我想让所有的Colun都充满值。但是当我有空列时,我想让它填充上一个NOTNULL列中的值Sql 如何用前一行中的值填充空列?,sql,google-bigquery,left-join,data-warehouse,gaps-and-islands,Sql,Google Bigquery,Left Join,Data Warehouse,Gaps And Islands,我想在这里表演一些东西。我想让所有的Colun都充满值。但是当我有空列时,我想让它填充上一个NOTNULL列中的值 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
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。
我想我在这里遗漏了什么,但我不知道是什么。这应该与您的
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
就问题而言,大查询不支持窗口函数中的
ignorenull
。这里有一个解决方案,它依赖于一个窗口max来定位保存最后一个非空的所有者id
(这假设时间戳是唯一的)。有了这些信息,您可以将相应的owner\u id
与join一起引入
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
:
timestamp | owner_id | owner_assigneddate | lastmodifieddate
:------------------ | -------: | :------------------ | :-------------------
2019-11-12 16:01:55 | null | null | null
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 | null | 2019-11-12 19:03:19
2019-11-12 19:03:20 | 39530934 | null | 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 | null | 2019-11-12 19:03:33
2019-11-12 19:03:56 | 40320368 | null | 2019-11-12 19:03:356
在BigQuery中,将
LAST_VALUE()
与ignorenulls
选项和COALESCE()
一起使用:
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
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