在分组后将行值显示为列 - Oracle

时间:2014-01-10 05:33:51

标签: oracle stored-procedures plsql oracle11g

这是我的查询

SELECT
  tbl_dtl_feature.customer_id,
  result,
  COUNT(*) AS expr1
FROM tbl_dtl_feature
WHERE tbl_dtl_feature.feature_id = 'F001'
      AND TRUNC(tbl_dtl_feature.start_datetime)
      BETWEEN TO_DATE('10/01/2014', 'DD/MM/YYYY') AND TO_DATE('10/01/2014', 'DD/MM/YYYY')
GROUP BY tbl_dtl_feature.result, tbl_dtl_feature.customer_id 

我的结果集:

CustomerID  Result  Count
----------  ------- -----
44438111    Success 3
44438444    Success 1
44438111    Failure 1

预期结果集:

CustomerID  Count(Success) Count(Failure)
----------  -------------- -------------
44438111       3              1
44438444       1              0
你可以帮帮我吗?

1 个答案:

答案 0 :(得分:2)

试试这个,

SELECT t.customer_id, 
       count(CASE WHEN result = 'Success' THEN 1 END) Count_Success, 
       count(CASE WHEN result = 'Failure' THEN 1 END) Count_Failure
FROM   tbl_dtl_feature t
WHERE  t.feature_id = 'F001'
AND    trunc(t.start_datetime) BETWEEN to_date('10/01/2014', 'DD/MM/YYYY') AND to_date('10/01/2014', 'DD/MM/YYYY') 
GROUP BY t.customer_id;