如何在窗口函数PostgreSQL中进行筛选



我只想在Window函数中过滤以保留created_at is null,这样我以前就不用WHERE子句了。因为当我order by created_at desc时,它将首先显示空值。

如何在此代码中添加筛选器?

select *, first_value(title) over (partition by product_id order by created_at desc)  as last_title
from t2

我试试这个not work:

select *, first_value(title) over (partition by product_id order by created_at desc) having (created_at is not null) as last_title
    from t2

您可以将NULL值排序到末尾:

first_value(title) over (partition by product_id order by created_at desc nulls last)

请使用以下条件,

select *, first_value(title) over (partition by product_id order by created_at desc)  
as last_title
from t2 where created_at is not null

最新更新