来自关键字未找到case语句中的预期位置

时间:2015-03-13 13:59:40

标签: sql case

我有一个查询,我在下面写。我收到了错误。我已经添加了双引号。还是行不通。请建议我。

SELECT  

TO_DATE(TO_CHAR(BATCH_CREATION_TIME,'YYYY/MM/DD'),'YYYY/MM/DD') AS CREATION_DATE, 
SUM(DECODE(CDR_ACTUAL_COUNT,NULL,0,CDR_ACTUAL_COUNT)) AS CCOLLECTED,
SUM(DECODE(CDR_ACTUAL_COUNT,NULL,0,CDR_ACTUAL_COUNT)) - SUM(DECODE(CDR_PARSE_ERROR_COUNT,NULL,0,CDR_PARSE_ERROR_COUNT))  
- SUM(DECODE(CDR_DISCARD_COUNT,NULL,0,CDR_DISCARD_COUNT)) AS COLLECTED,
SUM(DECODE(VALIDATION_CNR_COUNT,NULL,0,VALIDATION_CNR_COUNT)) + SUM(DECODE(VALIDATION_CE_COUNT,NULL,0,VALIDATION_CE_COUNT))   
+ SUM(DECODE(VALIDATION_CR_COUNT,NULL,0,VALIDATION_CR_COUNT)) + SUM(DECODE(VALIDATION_NCR_COUNT,NULL,0,VALIDATION_NCR_COUNT))  AS ERRORED, 
SUM(DECODE(VALIDATION_RNC_COUNT,NULL,0,VALIDATION_RNC_COUNT)) + SUM(DECODE(VALIDATION_RV_COUNT,NULL,0,VALIDATION_RV_COUNT)) AS PROCESSED 
"CDR_SOURCE_TYPE" =
CASE
WHEN CDR_SOURCE_TYPE IN ('SMSC') THEN DECODE(NETWORK_TYPE,'CDMA','SMSC_CDMA','HSPA','SMSC_HSPA') AS "CDR_SOURCE_TYPE"
WHEN CDR_SOURCE_TYPE IN ('P_GW') and BATCH_ID like '%_PGW_%' and BATCH_ID like '%_PGW_REPLAY_%' THEN 'PGW_REPLAY' AS "CDR_SOURCE_TYPE"
WHEN CDR_SOURCE_TYPE IN ('OCS_UDR','OCS_EDR','OCS_CDMA_EDR','OCS_UDR_RECOVER','MACH_UDR','AAA','PTT_CDMA','OCS_CDMA_UDR','MTX') THEN "CDR_SOURCE_TYPE"
--ELSE 'Excelent'
END

FROM  BATCH_SUMMARY

WHERE                 
BATCH_CREATION_TIME BETWEEN TO_DATE(TO_CHAR(sysdate,'YYYY-MM-DD') ||' 00:00:00', 'YYYY-MM-DD HH24:MI:SS')  
AND TO_DATE(TO_CHAR(sysdate,'YYYY-MM-DD') ||' 23:59:59', 'YYYY-MM-DD HH24:MI:SS') 
AND BATCH_CREATION_TIME < (SYSDATE -1/24)
--and CDR_SOURCE_TYPE IN ('OCS_UDR','OCS_EDR','OCS_CDMA_EDR','OCS_UDR_RECOVER','MACH_UDR','AAA','PTT_CDMA','OCS_CDMA_UDR','MTX')

GROUP BY 
TO_DATE(TO_CHAR(BATCH_CREATION_TIME,'YYYY/MM/DD'),'YYYY/MM/DD'), CDR_SOURCE_TYPE 

2 个答案:

答案 0 :(得分:0)

只关注案例陈述,而不是做

"CDR_SOURCE_TYPE" = (your case statement)

DO

(your case statement) as 'CDR_SOURCE_TYPE'

答案 1 :(得分:0)

看起来缺少comma

此外,您需要使用case中的整个group by语句而不是单独使用列名称

SELECT
To_date(To_char(BATCH_CREATION_TIME,'YYYY/MM/DD'),'YYYY/MM/DD') AS CREATION_DATE,
Sum(Decode(CDR_ACTUAL_COUNT,NULL,0,CDR_ACTUAL_COUNT)) AS CCOLLECTED,
Sum(Decode(CDR_ACTUAL_COUNT,NULL,0,CDR_ACTUAL_COUNT)) - Sum(Decode(CDR_PARSE_ERROR_COUNT,NULL,0,CDR_PARSE_ERROR_COUNT))
- Sum(Decode(CDR_DISCARD_COUNT,NULL,0,CDR_DISCARD_COUNT)) AS COLLECTED,
Sum(Decode(VALIDATION_CNR_COUNT,NULL,0,VALIDATION_CNR_COUNT)) + Sum(Decode(VALIDATION_CE_COUNT,NULL,0,VALIDATION_CE_COUNT))
+ Sum(Decode(VALIDATION_CR_COUNT,NULL,0,VALIDATION_CR_COUNT)) + Sum(Decode(VALIDATION_NCR_COUNT,NULL,0,VALIDATION_NCR_COUNT))  AS ERRORED,
Sum(Decode(VALIDATION_RNC_COUNT,NULL,0,VALIDATION_RNC_COUNT)) + Sum(Decode(VALIDATION_RV_COUNT,NULL,0,VALIDATION_RV_COUNT)) AS PROCESSED ,

CASE
WHEN CDR_SOURCE_TYPE IN ('SMSC') THEN Decode(NETWORK_TYPE,'CDMA','SMSC_CDMA','HSPA','SMSC_HSPA')
WHEN CDR_SOURCE_TYPE IN ('P_GW') AND BATCH_ID LIKE '%_PGW_%' AND BATCH_ID LIKE '%_PGW_REPLAY_%' THEN 'PGW_REPLAY'
WHEN CDR_SOURCE_TYPE IN ('OCS_UDR','OCS_EDR','OCS_CDMA_EDR','OCS_UDR_RECOVER','MACH_UDR','AAA','PTT_CDMA','OCS_CDMA_UDR','MTX') THEN CDR_SOURCE_TYPE
--ELSE 'Excelent'
END AS CDR_SOURCE_TYPE
FROM  BATCH_SUMMARY

WHERE
BATCH_CREATION_TIME BETWEEN To_date(To_char(sysdate,'YYYY-MM-DD') ||' 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
AND To_date(To_char(sysdate,'YYYY-MM-DD') ||' 23:59:59', 'YYYY-MM-DD HH24:MI:SS')
AND BATCH_CREATION_TIME < (SYSDATE -1/24)
--and CDR_SOURCE_TYPE IN ('OCS_UDR','OCS_EDR','OCS_CDMA_EDR','OCS_UDR_RECOVER','MACH_UDR','AAA','PTT_CDMA','OCS_CDMA_UDR','MTX')

GROUP BY
TO_DATE(TO_CHAR(BATCH_CREATION_TIME,'YYYY/MM/DD'),'YYYY/MM/DD'), 
CASE
WHEN CDR_SOURCE_TYPE IN ('SMSC') THEN Decode(NETWORK_TYPE,'CDMA','SMSC_CDMA','HSPA','SMSC_HSPA') AS "CDR_SOURCE_TYPE"
WHEN CDR_SOURCE_TYPE IN ('P_GW') AND BATCH_ID LIKE '%_PGW_%' AND BATCH_ID LIKE '%_PGW_REPLAY_%' THEN 'PGW_REPLAY' AS "CDR_SOURCE_TYPE"
WHEN CDR_SOURCE_TYPE IN ('OCS_UDR','OCS_EDR','OCS_CDMA_EDR','OCS_UDR_RECOVER','MACH_UDR','AAA','PTT_CDMA','OCS_CDMA_UDR','MTX') THEN "CDR_SOURCE_TYPE"
--ELSE 'Excelent'
END

还要确保数据库中支持Aliasname=column_name格式。在Sql server中它可以工作。否则使用

Column_name as Aliasname 

<强>更新

SELECT CASE -- CDR_SOURCE_TYPE removed
         WHEN CDR_SOURCE_TYPE IN ( 'SMSC' ) THEN Decode(NETWORK_TYPE, 'CDMA', 'SMSC_CDMA',
                                                                      'HSPA', 'SMSC_HSPA')
         WHEN CDR_SOURCE_TYPE IN ( 'P_GW' )
              AND BATCH_ID LIKE '%_PGW_%'
              AND BATCH_ID LIKE '%_PGW_REPLAY_%' THEN 'PGW_REPLAY'
       END
FROM   BATCH_SUMMARY