在SELECT中按CASE分组

时间:2018-07-19 01:46:36

标签: sql oracle

嗨,我有一个类似以下的查询,我如何分组?

 select distinct
 CTTA.TRANSACTION_TYPE "Transaction Type"
, 'Quota1' "Quota Type"
, CTTA.ATTRIBUTE2 "PO Number"
, (CASE WHEN CTTA.TRANSACTION_TYPE='SFDC'
            THEN CTTA.ATTRIBUTE1
            ELSE CTTA.ATTRIBUTE39
    END
    ) "Customer"
, CTTA.ATTRIBUTE75 "Opportunity Name"
, CTTA.ATTRIBUTE63 "Comm Norm"
, CTTA.ATTRIBUTE8 "Renewal"
,SUM(CTTA.TRANSACTION_AMT_FUNC_CURR) "Transaction Amount"
from xxxxxx
where xxxxxx
GROUP BY 1,2,3,4,5,6,7

1 个答案:

答案 0 :(得分:3)

Oracle在group by中既不支持位置别名也不支持列别名。您可以重复列表达式:

 select CTTA.TRANSACTION_TYPE as "Transaction Type"
        'Quota1' as "Quota Type", 
        CTTA.ATTRIBUTE2 "PO Number",
        (CASE WHEN CTTA.TRANSACTION_TYPE='SFDC'
              THEN CTTA.ATTRIBUTE1
              ELSE CTTA.ATTRIBUTE39
         END) as "Customer",
        CTTA.ATTRIBUTE75 as "Opportunity Name",
        CTTA.ATTRIBUTE63 as "Comm Norm",
        CTTA.ATTRIBUTE8 as "Renewal",
        SUM(CTTA.TRANSACTION_AMT_FUNC_CURR) as "Transaction Amount"
from xxxxxx
where xxxxxx
GROUP BY CTTA.TRANSACTION_TYPE, CTTA.ATTRIBUTE2,
         (CASE WHEN CTTA.TRANSACTION_TYPE='SFDC'
               THEN CTTA.ATTRIBUTE1
               ELSE CTTA.ATTRIBUTE39
          END),
         CTTA.ATTRIBUTE75,
         CTTA.ATTRIBUTE63,
         CTTA.ATTRIBUTE75,
         CTTA.ATTRIBUTE8;