2012-09-27 53 views
1

我正在嘗試做一個查詢,它在一列中計算prorata,完整和隨機結果的數量,並將它們顯示在3個分開的列中。你可以給每個case語句一個列別名嗎? SQL,pl/sql

到目前爲止,我想:

SELECT ot.ort_method, 
CASE ot.ort_method 
    WHEN 'PRORATE' THEN '1' AS 'Pro_Rata' 
    WHEN 'COMPLETE' THEN '2' AS 'Complete' 
    WHEN 'RANDOM' THEN '3' AS 'Random' 
END 
FROM orders o, order_tranches ot 
WHERE o.ord_deal_code = 'EM0004357P'; 

不工作。有人知道該怎麼做嗎?

回答

2

的樣本數據:

with t1 as(
    select 'EM0004357P' ord_deal_code, 'PRORATE' ort_method from dual union all 
    select 'EM0004357P' ord_deal_code, 'COMPLETE' ort_method from dual union all 
    select 'EM0004357P' ord_deal_code, 'PRORATE' ort_method from dual union all 
    select 'EM0004357P' ord_deal_code, 'RANDOM' ort_method from dual union all 
    select 'EM0004357P' ord_deal_code, 'RANDOM' ort_method from dual 
) 

查詢:

SELECT count(decode(o.ort_method, 'PRORATE', 1)) as prorate 
    , count(decode(o.ort_method, 'COMPLETE', 1)) as complete 
    , count(decode(o.ort_method, 'RANDOM', 1)) as random 
FROM t1 o 
WHERE o.ord_deal_code = 'EM0004357P'; 

結果:

Prorate complete random 
    2   1  2 
+0

這工作完美。謝謝! – Mac