Last Non-Null Value in Redshift by Group

守給你的承諾、 提交于 2020-01-16 01:47:08

问题


I am using Redshift and want to receive the last non-Null value by userid.

Here is an example dataset:

     Date     UserID      Value
4-18-2018        abc          1
4-19-2018        abc       NULL
4-20-2018        abc       NULL
4-21-2018        abc          8
4-19-2018        def          9
4-20-2018        def         10
4-21-2018        def       NULL
4-22-2018        tey       NULL
4-23-2018        tey          2

If the new user starts out with a NULL then replace with 0.

I want my final dataset to look like this:

     Date     UserID      Value
4-18-2018        abc          1
4-19-2018        abc          1
4-20-2018        abc          1
4-21-2018        abc          8
4-19-2018        def          9
4-20-2018        def         10
4-21-2018        def         10
4-22-2018        tey          1
4-23-2018        tey          2

Any help would be great thanks!


回答1:


You can do this with lag() and the ignore nulls option:

select date, userid,
       coalesce(value, lag(value ignore nulls) over (partition by userid order by date)) as value
from t;

If the values are increasing, you can also use a cumulative maximum:

select date, userid,
       max(value) over (partition by userid order by date) as value
from t;


来源:https://stackoverflow.com/questions/49732956/last-non-null-value-in-redshift-by-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!