用 postgresql 中分区的最后一个值填充列 [英] fill column with last value from partition in postgresql

查看:31
本文介绍了用 postgresql 中分区的最后一个值填充列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试返回分区的最后一个值并将其应用于列的其余部分

I'm trying to return the last value of a partition and apply it to the rest of the column

例如,如果我有以下...

For example, if I have the below...

ID    Date      Status
1     20150101
1     20150201
1     20150301
1     20150401  void
2     20150101
2     20150201
2     20150301 

我想退货.

ID    Date      Status
1     20150101   void
1     20150201   void
1     20150301   void
1     20150401   void
2     20150101
2     20150201
2     20150301  

我一直在玩下面的东西,但无济于事.

I've been playing around with the below and similar to no avail.

select 
ID, 
date, 
case when status is null then last_value(status ignore nulls) 
over (partition by id order by id, date) else status end as status
from table

任何帮助将不胜感激.

推荐答案

你不需要 CASE 语句:

SELECT id, date, last_value(status) OVER (PARTITION BY id ORDER BY date) AS stat
FROM table;

这篇关于用 postgresql 中分区的最后一个值填充列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆