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 回答