Default RANGE / ROWS
for FIRST_VALUE
(对于任何其他分析函数)是BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
.
如果你添加IGNORE NULLS
, then NULL
构建范围时不考虑值。
The RANGE
变成BETWEEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCEPT FOR THE NULL ROWS
(这不是有效的OVER
条款)。
自从你的txt
那是NULL
有高id
的,它们首先被选择,并且它们的范围是空的,因为没有非NULL
它们之间的行和UNBOUNDED PRECEDING
你应该改变ORDER BY
or RANGE
您的查询的子句。
改变ORDER BY
将行与NULL
id 到窗口的末尾,以便非NULL
值(如果有)将始终首先被选择,并且RANGE
肯定会从该值开始:
with t
as (
select 450 id, null txt , 3488 id_usr from dual union all
select 449 , null , 3488 from dual union all
select 79 , 'A' , 3488 from dual union all
select 78 , 'X' , 3488 from dual
)
select id
, txt
, id_usr
, first_value(txt) over (partition by id_usr order by NVL2(TXT, NULL, id) DESC) first_one
from t
改变RANGE
重新定义范围以包括所有非NULL
分区中的行:
with t
as (
select 450 id, null txt , 3488 id_usr from dual union all
select 449 , null , 3488 from dual union all
select 79 , 'A' , 3488 from dual union all
select 78 , 'X' , 3488 from dual
)
select id
, txt
, id_usr
, first_value(txt IGNORE NULLS) over (partition by id_usr order by id DESC RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) first_one
from t