1

こんにちは私は動作しているDB2のSQLステートメントを持っています。

select  distinct 'IN' as STATUS,
    (select count(*) from table.......)
from table

UNION ALL

select  distinct 'OUT',
   (select count(*) from table.......)
from table

UNION ALL

select  distinct 'FINISHED',
   (select count(*) from table.......)
from table

order by status

しかし、最後の行をに変更すると

order by 
 case STATUS
 when 'IN' then 1
 when 'OUT' then 2
 when 'FINISHED' then 3
end

クエリが機能しません。誰かがこれを解決する方法を教えてもらえますか?ありがとう

4

3 に答える 3

3

UNION を派生テーブルにラップしてみてください。

select *
from ( 
   .... here goes your statement ...
) t
order by 
   case STATUS
      when 'IN' then 1
      when 'OUT' then 2
      when 'FINISHED' then 3
   end
于 2012-06-02T22:51:13.313 に答える
1

ステータスに並べ替え # をいつでも追加できます。

select  distinct '1-IN' as STATUS,
    (select count(*) from table.......)
from table

UNION ALL

select  distinct '2-OUT',
   (select count(*) from table.......)
from table

UNION ALL

select  distinct '3-FINISHED',
   (select count(*) from table.......)
from table

order by status
于 2012-06-02T23:00:24.073 に答える
0

こんにちは、私が正しく覚えていれば、これはうまくいくはずです

    order by 
     case 
      when STATUS='IN' then 1
      when STATUS='OUT' then 2
      when STATUS='FINISHED' then 3
     end

最後に field_name として終了するときに、これに名前を付けることもできます

于 2012-06-02T22:44:46.997 に答える