2012-06-01 29 views
0

我想进行自定义订单:IN,OUT,FINISHED。
如果我遗留了病例陈述,我正在完成FIN,IN,OUT。 我发现这个解决方案,但它不适合我 - 我得到错误。明确陈述后的自定义订单

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

UNION ALL 

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

UNION ALL 

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

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

您应该发布这样的问题的逐字错误消息。 –

回答

1

您提供的查询有一些语法不规范。我认为以下解决您的问题:

select * 
from ((select distinct 'IN' as statusA, (select count(*) ... 
     from table 
    ) 
     union all 
     (select distinct 'OUT', (select count(*) ...) 
     from table 
    ) 
     union all 
     (select distinct 'FINISHED', (select count(*) ...) 
     from table 
    ) 
    ) t 
order by status, 
     (case STATUSA when 'IN' then 1 
        when 'OUT' then 2 
        when 'FINISHED' then 3 
     end) 

您原来的问题可能有几个原因。你错过了第一个子查询中的'IN'。您在顺序状态后缺少逗号。而且,有些数据库通过一系列联合将最终的订单应用于最后一个查询(尽管我认为DB2能够正确执行此操作)。