1

問題を可能な限り単純化します。

私はオラクルテーブルを持っています:

row_priority, col1, col2, col3
0, .1, 100, {null}
12, {null}, {null}, 3
24, .2, {null}, {null}

望ましい結果:

col1, col2, col3
.2, 100, 3

したがって、行の優先度に従って、指定されている場合は前の行の値をオーバーライドします。

テーブル上で分析関数を使用してソリューションを作成しようとしていますが、動作していません...

私は試してみます:

select last_value(col1 ignore nulls) over () col1,
       last_value(col2 ignore nulls) over () col2,
       last_value(col3 ignore nulls) over () col3
from (select * from THE_TABLE order by row_priority)
where rownum = 1

またはその逆:

select first_value(col1 ignore nulls) over () col1,
       first_value(col2 ignore nulls) over () col2,
       first_value(col3 ignore nulls) over () col3
from (select * from THE_TABLE order by row_priority desc)
where rownum = 1

そして、どちらもnullを無視していないようです。ヒントはありますか?

4

3 に答える 3

2

rownum = 1 を分析クエリの外側に配置する必要があります

SELECT  *
FROM    (   select          last_value(col1 ignore nulls) over () col1,
                            last_value(col2 ignore nulls) over () col2,
                            last_value(col3 ignore nulls) over () col3
            from (select * from THE_TABLE ORDER BY ROW_PRIORITY)
        )
WHERE   ROWNUM = 1

その結果(上記の値を使用):

COL1   COL2    COL3
------ ------- ----
0.2    100     3
于 2008-11-04T14:14:22.387 に答える
-1

COALESCE関数は、ここで役立つ場合があります。おそらく...

select first_value(coalesce(col1,0) ignore nulls) over () col1,
       first_value(coalesce(col2,0) ignore nulls) over () col2,
       first_value(coalesce(col3,0) ignore nulls) over () col3
from THE_TABLE
于 2008-11-04T13:47:14.483 に答える
-1

別の方法:

SELECT
  MAX(col1) KEEP (DENSE_RANK LAST ORDER BY row_priority),
  MAX(col2) KEEP (DENSE_RANK LAST ORDER BY row_priority),
  MAX(col3) KEEP (DENSE_RANK LAST ORDER BY row_priority)
FROM the_table

このパフォーマンスは、分析バージョンとは異なる場合があります。良いか悪いかは、データと環境によって異なります。

于 2008-11-04T18:20:54.277 に答える