样本数据
CREATE TABLE test
(id integer, session_ID integer, value integer)
;
INSERT INTO test
(id, session_ID, value)
VALUES
(0, 2, 100),
(1, 2, 120),
(2, 2, 140),
(3, 1, 900),
(4, 1, 800),
(5, 1, 500)
;
Run Code Online (Sandbox Code Playgroud)
当前查询
select
id,
last_value(value) over (partition by session_ID order by id) as last_value_window,
last_value(value) over (partition by session_ID order by id desc) as last_value_window_desc
from test
ORDER BY id
Run Code Online (Sandbox Code Playgroud)
我在使用last_value()
窗口函数时遇到问题:http :
//sqlfiddle.com/#!15/bcec0/2
在小提琴中,我尝试使用last_value()
查询中的排序方向。
编辑:
问题不在于:为什么我没有得到所有时间的最后一个值,以及如何使用frame子句(unbounded preceding
和 …
我知道已经有人问过这个问题,但为什么下面的解决方案不起作用?我想填充value
按 排序的最后一个非空值idx
。
我所看到的:
idx | coalesce
-----+----------
1 | 2
2 | 4
3 |
4 |
5 | 10
(5 rows)
Run Code Online (Sandbox Code Playgroud)
我想要的是:
idx | coalesce
-----+----------
1 | 2
2 | 4
3 | 4
4 | 4
5 | 10
(5 rows)
Run Code Online (Sandbox Code Playgroud)
代码:
with base as (
select 1 as idx
, 2 as value
union
select 2 as idx
, 4 as value
union
select 3 as idx
, null as value
union …
Run Code Online (Sandbox Code Playgroud)