PostgreSQL窗口函数忽略空值
我对PostgreSQL比较陌生,在忽略空值的情况下很难实现lag()函数。根据文档,不可能简单地将参数传递给函数来实现这一点 有人知道有什么解决办法吗?你可以举出任何一个例子,但如果它有助于你开始:PostgreSQL窗口函数忽略空值,postgresql,lag,Postgresql,Lag,我对PostgreSQL比较陌生,在忽略空值的情况下很难实现lag()函数。根据文档,不可能简单地将参数传递给函数来实现这一点 有人知道有什么解决办法吗?你可以举出任何一个例子,但如果它有助于你开始: Col_A Col_B Col_A_Lag Col_B_Lag ID Date VA_1 100 VA_1 100 AA Jan-1 null null VA_1 100 AA Jan-2 null
Col_A Col_B Col_A_Lag Col_B_Lag ID Date
VA_1 100 VA_1 100 AA Jan-1
null null VA_1 100 AA Jan-2
null null VA_1 100 AA Jan-3
VA_2 250 VA_2 250 AA Jan-4
VA_2 300 VA_2 300 AA Jan-5
null null VA_2 300 AA Jan-6
如果我是从tbl_x中提取,下面是一个简单的SQL脚本:
select
Col_A,
Col_B,
case when Col_A is null then lag(Col_A) over w else Col_A end as Col_A_Lag,
case when Col_B is null then lag(Col_B) over w else Col_B end as Col_B_Lag
from tbl_x
window w as (partition by ID order by Date)
这个脚本不会返回我想要的结果,因为它在延迟中“回望”时包含空值
提前谢谢。我想你不能只是:
select
from tbl_x
window w as (partition by ID order by Date)
where col_a is null;
如果没有,您可能必须:
select
Col_A,
Col_B,
case when Col_A is null
then (select col_a
from tbl_x x2
where x2.id = x1.id
and col_a is not null
and x2.date < x1.date
order by date desc
limit 1)
else Col_A
end Col_A_Lag,
case when Col_B is null
then (select col_b
from tbl_x x2
where x2.id = x1.id
and col_b is not null
and x2.date < x1.date
order by date desc
limit 1)
else Col_B
end Col_B_Lag
from tbl_x x1;
我想你不能只是:
select
from tbl_x
window w as (partition by ID order by Date)
where col_a is null;
如果没有,您可能必须:
select
Col_A,
Col_B,
case when Col_A is null
then (select col_a
from tbl_x x2
where x2.id = x1.id
and col_a is not null
and x2.date < x1.date
order by date desc
limit 1)
else Col_A
end Col_A_Lag,
case when Col_B is null
then (select col_b
from tbl_x x2
where x2.id = x1.id
and col_b is not null
and x2.date < x1.date
order by date desc
limit 1)
else Col_B
end Col_B_Lag
from tbl_x x1;
我想你不能只是:
select
from tbl_x
window w as (partition by ID order by Date)
where col_a is null;
如果没有,您可能必须:
select
Col_A,
Col_B,
case when Col_A is null
then (select col_a
from tbl_x x2
where x2.id = x1.id
and col_a is not null
and x2.date < x1.date
order by date desc
limit 1)
else Col_A
end Col_A_Lag,
case when Col_B is null
then (select col_b
from tbl_x x2
where x2.id = x1.id
and col_b is not null
and x2.date < x1.date
order by date desc
limit 1)
else Col_B
end Col_B_Lag
from tbl_x x1;
我想你不能只是:
select
from tbl_x
window w as (partition by ID order by Date)
where col_a is null;
如果没有,您可能必须:
select
Col_A,
Col_B,
case when Col_A is null
then (select col_a
from tbl_x x2
where x2.id = x1.id
and col_a is not null
and x2.date < x1.date
order by date desc
limit 1)
else Col_A
end Col_A_Lag,
case when Col_B is null
then (select col_b
from tbl_x x2
where x2.id = x1.id
and col_b is not null
and x2.date < x1.date
order by date desc
limit 1)
else Col_B
end Col_B_Lag
from tbl_x x1;
我很感激你的回答,但是当Colu_A发生变化时,你的第二个解决方案不起作用。根据我对“日期”的排序方式,我要么得到列A_滞后中的所有VA_1值,要么得到列A_滞后中的VA_2值。啊,我没有发现这一点逻辑。已编辑查询以将其包括在内。关闭。。。所有空值都用VA_1…填充。。。。最后一个应该是VA_2。谢谢。我很感谢你的回答,但是当上校改变时,你的第二个解决方案不起作用。根据我对“日期”的排序方式,我要么得到列A_滞后中的所有VA_1值,要么得到列A_滞后中的VA_2值。啊,我没有发现这一点逻辑。已编辑查询以将其包括在内。关闭。。。所有空值都用VA_1…填充。。。。最后一个应该是VA_2。谢谢。我很感谢你的回答,但是当上校改变时,你的第二个解决方案不起作用。根据我对“日期”的排序方式,我要么得到列A_滞后中的所有VA_1值,要么得到列A_滞后中的VA_2值。啊,我没有发现这一点逻辑。已编辑查询以将其包括在内。关闭。。。所有空值都用VA_1…填充。。。。最后一个应该是VA_2。谢谢。我很感谢你的回答,但是当上校改变时,你的第二个解决方案不起作用。根据我对“日期”的排序方式,我要么得到列A_滞后中的所有VA_1值,要么得到列A_滞后中的VA_2值。啊,我没有发现这一点逻辑。已编辑查询以将其包括在内。关闭。。。所有空值都用VA_1…填充。。。。最后一个应该是VA_2。谢谢