デフォルトのRANGE / ROWS FIRST_VALUEの場合 (他の分析関数と同様に)BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 。
IGNORE NULLSを追加した場合 、次にNULL 範囲を作成するときに値は考慮されません。
RANGE BETWEEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCEPT FOR THE NULL ROWSになります (有効なOVERではありません 条項)。
txt以降 NULLである idが高い の場合、最初に選択され、NULL以外のコードがないため、範囲は空です。 それらとUNBOUNDED PRECEDINGの間の行
ORDER BYのいずれかを変更する必要があります または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