case语句中的多个条件

时间:2017-10-04 04:40:46

标签: teradata

我已经编写了以下代码,它按预期工作但是花费了太多时间。

    `SELECT COUNT(SRC.DVC_ID),'AUDIO' AS FEATURE,'PDPN                                      ' AS COL_NAME, 'DP_VEDW_TRM.V_DEVICE_VIEW_SUMMARY ' AS SRC_TABLE,'DP_VEDW_SRC_MDB_NGN.DVC_SUM_AUDO_VIEW_HIST_PT' AS TGT_TABLE
    FROM DP_VEDW_TRM.V_DEVICE_VIEW_SUMMARY SRC
    JOIN DP_VEDW_SRC_MDB_NGN.DVC_SUM_AUDO_VIEW_HIST_PT TGT
    ON SRC.DVC_ID=TGT.DVC_ID AND SRC.PDPN<>TGT.PDPN
    WHERE TGT.CUR_REC_FL='Y' AND SRC.AUDO_STS IN('A','S') AND SRC.AUDO_PROD_ID IS NOT NULL AND UPPER(SRC.DELV_METH)<>'TELEMATICS' 
    UNION ALL        
    SELECT COUNT(SRC.DVC_ID),'AUDIO' AS FEATURE,'PSN' AS COL_NAME, 'DP_VEDW_TRM.V_DEVICE_VIEW_SUMMARY ' AS SRC_TABLE,'DP_VEDW_SRC_MDB_NGN.DVC_SUM_AUDO_VIEW_HIST_PT' AS TGT_TABLE
    FROM DP_VEDW_TRM.V_DEVICE_VIEW_SUMMARY SRC
    JOIN DP_VEDW_SRC_MDB_NGN.DVC_SUM_AUDO_VIEW_HIST_PT TGT
    ON SRC.DVC_ID=TGT.DVC_ID AND SRC.PSN<>TGT.PSN
    WHERE TGT.CUR_REC_FL='Y' AND SRC.AUDO_STS IN('A','S') AND SRC.AUDO_PROD_ID IS NOT NULL AND UPPER(SRC.DELV_METH)<>'TELEMATICS' 
    UNION ALL                 
    SELECT COUNT(SRC.DVC_ID),'AUDIO' AS FEATURE,'PTNR_ID' AS COL_NAME, 'DP_VEDW_TRM.V_DEVICE_VIEW_SUMMARY ' AS SRC_TABLE,'DP_VEDW_SRC_MDB_NGN.DVC_SUM_AUDO_VIEW_HIST_PT' AS TGT_TABLE
    FROM DP_VEDW_TRM.V_DEVICE_VIEW_SUMMARY SRC
    JOIN DP_VEDW_SRC_MDB_NGN.DVC_SUM_AUDO_VIEW_HIST_PT TGT
    ON SRC.DVC_ID=TGT.DVC_ID AND SRC.PTNR_ID<>TGT.PTNR_ID
    WHERE TGT.CUR_REC_FL='Y' AND SRC.AUDO_STS IN('A','S') AND SRC.AUDO_PROD_ID IS NOT NULL AND UPPER(SRC.DELV_METH)<>'TELEMATICS' 
    UNION ALL                  
    SELECT COUNT(SRC.DVC_ID),'AUDIO' AS FEATURE,'PTNR_NM' AS COL_NAME, 'DP_VEDW_TRM.V_DEVICE_VIEW_SUMMARY ' AS SRC_TABLE,'DP_VEDW_SRC_MDB_NGN.DVC_SUM_AUDO_VIEW_HIST_PT' AS TGT_TABLE
    FROM DP_VEDW_TRM.V_DEVICE_VIEW_SUMMARY SRC
    JOIN DP_VEDW_SRC_MDB_NGN.DVC_SUM_AUDO_VIEW_HIST_PT TGT
    ON SRC.DVC_ID=TGT.DVC_ID AND SRC.PTNR_NM<>TGT.PTNR_NM
    WHERE TGT.CUR_REC_FL='Y' AND SRC.AUDO_STS IN('A','S') AND SRC.AUDO_PROD_ID IS NOT NULL AND UPPER(SRC.DELV_METH)<>'TELEMATICS' 
    UNION ALL              
    SELECT COUNT(SRC.DVC_ID),'AUDIO' AS FEATURE,'SEC_PTNR_ID' AS COL_NAME, 'DP_VEDW_TRM.V_DEVICE_VIEW_SUMMARY ' AS SRC_TABLE,'DP_VEDW_SRC_MDB_NGN.DVC_SUM_AUDO_VIEW_HIST_PT' AS TGT_TABLE
    FROM DP_VEDW_TRM.V_DEVICE_VIEW_SUMMARY SRC
    JOIN DP_VEDW_SRC_MDB_NGN.DVC_SUM_AUDO_VIEW_HIST_PT TGT
    ON SRC.DVC_ID=TGT.DVC_ID AND SRC.SEC_PTNR_ID<>TGT.SEC_PTNR_ID
    WHERE TGT.CUR_REC_FL='Y' AND SRC.AUDO_STS IN('A','S') AND SRC.AUDO_PROD_ID IS NOT NULL AND UPPER(SRC.DELV_METH)<>'TELEMATICS' `

所以我修改了下面的查询,但输出错误。

SELECT * FROM(
    SELECT COUNT(SRC.DVC_ID)OVER(PARTITION BY COL_NAME )  AS DVC_ID_CNT,
    'TRFC' AS FEATURE,
    CASE WHEN SRC.PDPN<>TGT.PDPN THEN 'PDPN                                                 ' 
    WHEN SRC.PSN<>TGT.PSN THEN 'PSN                                                 ' 
     WHEN SRC.PTNR_ID<>TGT.PTNR_ID THEN 'PTNR_ID' 
     WHEN SRC.SEC_PTNR_ID<>TGT.SEC_PTNR_ID THEN 'SEC_PTNR_ID' 
     WHEN SRC.SEC_PTNR_ID<>TGT.SEC_PTNR_ID THEN 'SEC_PTNR_ID' ELSE '0' END COL_NAME
    ,'DP_VEDW_TRM.V_DEVICE_VIEW_SUMMARY' AS SRC_TABLE,'DP_VEDW_SRC_MDB_NGN.DVC_SUM_AUDO_VIEW_HIST_PT' AS TGT_TABLE
    FROM DP_VEDW_TRM.V_DEVICE_VIEW_SUMMARY SRC
    JOIN DP_VEDW_SRC_MDB_NGN.DVC_SUM_AUDO_VIEW_HIST_PT TGT ON SRC.DVC_ID=TGT.DVC_ID
    WHERE TGT.CUR_REC_FL='Y' AND SRC.AUDO_STS IN('A','S') AND SRC.AUDO_PROD_ID IS NOT NULL AND UPPER(SRC.DELV_METH)<>'TELEMATICS'  
    AND  COL_NAME<>'0'
    )A GROUP BY 1,2,3,4,5

请建议。 提前致谢

1 个答案:

答案 0 :(得分:0)

这个查询过于复杂,不需要COUNT OVER和GROUP BY,简单的COUNT就足够了。

假设单行可以满足多个WHEN,结果将是错误的,因为CASE不能用于返回多个值/行,它总是第一个匹配。

在表格中单次传递的唯一方法是旋转:

SELECT
   Count(CASE WHEN SRC.PDPN<>TGT.PDPN               THEN 1 END) AS PDPN,
   Count(CASE WHEN SRC.PSN<>TGT.PSN                 THEN 1 END) AS PSN,
   Count(CASE WHEN SRC.PTNR_ID<>TGT.PTNR_ID         THEN 1 END) AS PTNR_ID,
   Count(CASE WHEN SRC.SEC_PTNR_ID<>TGT.SEC_PTNR_ID THEN 1 END) AS SEC_PTNR_ID,
   'TRFC' AS FEATURE,
  ,'DP_VEDW_TRM.V_DEVICE_VIEW_SUMMARY' AS SRC_TABLE
  ,'DP_VEDW_SRC_MDB_NGN.DVC_SUM_AUDO_VIEW_HIST_PT' AS TGT_TABLE
FROM   DP_VEDW_TRM.V_DEVICE_VIEW_SUMMARY SRC
JOIN DP_VEDW_SRC_MDB_NGN.DVC_SUM_AUDO_VIEW_HIST_PT TGT 
  ON SRC.DVC_ID=TGT.DVC_ID
WHERE  TGT.CUR_REC_FL='Y' 
  AND SRC.AUDO_STS IN('A','S') 
  AND SRC.AUDO_PROD_ID IS NOT NULL 
  AND Upper(SRC.DELV_METH)<>'TELEMATICS'  

当然你可以使用WITH来重新打开这个以匹配第一个结果:-)

WITH cte AS
 (
   previous query plus DISTINCT
   !!!must add DISTINCT to force the optimizer to materialize the result!!!
 )
SELECT PDPN, 'PDPN', FEATURE, SRC_TABLE, TGT_TABLE FROM cte
UNION ALL 
SELECT PSN, 'PSN', FEATURE, SRC_TABLE, TGT_TABLE FROM cte
UNION ALL
...