1

我正在尝试进行查询,它计算一列中按比例、完整和随机结果的数量,并将它们显示在 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';

这是行不通的。有人知道怎么做吗?

4

1 回答 1

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 
于 2012-09-27T11:15:48.153 回答