[pgsql-jp: 39900] nth_value関数(パーティション切りあり)の結果がおかしい

postgresqlmailabcd @ mail.goo.ne.jp postgresqlmailabcd @ mail.goo.ne.jp
2009年 6月 24日 (水) 19:47:23 JST


nth_value関数のignore nullsを代用しようとしたら、
こんな現象に遭遇しました。
原因が分かる方がおられましたら、教えてください。

select version();
                            version
----------------------------------------------------------------
 PostgreSQL 8.4beta2, compiled by Visual C++ build 1400, 32-bit

-- nth_valueが正しい結果を返すSQL
with W(ID,Val) as(
select 1,null union all
select 1, 999 union all
select 1, 888 union all
select 1, 777 union all
select 2,null union all
select 2, 666)
select ID,Val,
nth_value(Val,3) over(partition by ID,Val is null) as igno_null
  from W
order by ID,Val desc;

 id | val | igno_null
----+-----+-----------
  1 |     |
  1 | 999 |       777
  1 | 888 |       777
  1 | 777 |       777
  2 |     |
  2 | 666 |

-- nth_valueが間違った結果を返すSQL
with W(ID,Val) as(
select 1,null union all
select 1, 999 union all
select 1, 888 union all
select 1, 777 union all
select 2,null union all
select 2,null union all /* I added this row */
select 2, 666)
select ID,Val,
nth_value(Val,3) over(partition by ID,Val is null) as igno_null
  from W
order by ID,Val desc;

 id | val | igno_null
----+-----+-----------
  1 |     |
  1 | 999 |       888
  1 | 888 |       888
  1 | 777 |       888
  2 |     |
  2 |     |
  2 | 666 |

I added this rowの行を追加したら、結果が変わってしまいます。
igno_nullは888ではなく、777が正しいはずです。

Val is null
を
case when Val is null then 1 else 0 end
に変更しても結果は888のままでした。

**************************************************************
明智重蔵のブログ
http://oraclesqlpuzzle.hp.infoseek.co.jp/blog.html




pgsql-jp メーリングリストの案内