以下の内容はhttps://unageanu.hatenablog.com/entry/20081106/1225961940より取得しました。


ソート時にNULLを最小値扱いにする

Unofficial DB2 BLOG - ORDER BYでNULLを最小に持ってくるにはを参考にさせていただいて、以下のように書いていたのだけど、

select * from A 
order by case when id is null then 0 else 1 end, id

selectにdistinctをつけるとエラーに・・・

$ db2 "select distinct  * from A order by case when id is null then 0 else 1 end, id"
SQL0214N  An expression in the ORDER BY clause in the following position, or 
starting with "case..." in the "ORDER BY" clause is not valid.  Reason code = 
"2".  SQLSTATE=42822

副選択-order-by-clauseの説明を見ると、「sort-key-expressionは、DISTINCTが副選択の SELECT 文節に指定されている場合使えない」というようなことが書いてある(ように読める)。うーん。なんでだろ。

とりあえず↑のエラーは以下のようにすることで回避できるけど

select * from 
  ( select distinct * from A ) as X 
order by case when id is null then 0 else 1 end, id

プログラムの作り的に今から↑のように変更するの結構面倒だなー、どうするかな。




以上の内容はhttps://unageanu.hatenablog.com/entry/20081106/1225961940より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14