相关疑难解决方法(0)

窗口函数:last_value(ORDER BY ... ASC)与last_value(ORDER BY ... DESC)相同

样本数据

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和 …

sql postgresql window-functions

7
推荐指数
2
解决办法
2733
查看次数

PostgreSQL Last_value 忽略空值

我知道已经有人问过这个问题,但为什么下面的解决方案不起作用?我想填充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)

sql postgresql null window-functions

5
推荐指数
3
解决办法
6805
查看次数

标签 统计

postgresql ×2

sql ×2

window-functions ×2

null ×1