你能给每个case语句一个列别名吗? SQL,pl / sql

时间:2012-09-27 11:08:09

标签: sql plsql case alias

我正在尝试进行查询,其中计算列中的分数,完整和随机结果的数量,并将其显示在3个单独的列中。

到目前为止,我正在尝试:

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';

哪个不起作用。有人知道怎么做吗?

1 个答案:

答案 0 :(得分: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