Teradata聚合函数GROUP BY

时间:2015-11-18 14:13:44

标签: aggregate teradata

嗨我收到错误,因为GROUP BY和WITH BY CLAUSE可能不会包含以下查询的聚合函数。

SELECT 
distinct CC.CASE_ID as CASE_ID,
/*FIRST_VALUE(CC.CASE_OWN_NM) OVER(PARTITION BY CC.CASE_ID )as  FST_AGNT_CASE_OWN_NM,
FIRST_VALUE(CC.LSTMOD_BY_AGNT_PRFL_NM) OVER(PARTITION BY CC.CASE_ID)as  FST_AGNT_PRFL_NM,
LAST_VALUE(CC.CASE_OWN_NM) OVER(PARTITION BY CC.CASE_ID) as     LST_AGNT_CASE_OWN_NM,
LAST_VALUE(CC.LSTMOD_BY_AGNT_PRFL_NM) OVER(PARTITION BY CC.CASE_ID)  as  LST_AGNT_PRFL_NM,*/
case when CC.CASE_OWN_NM is not null then MIN(CC.REC_DTTM_PST) end as FST_AGNT_EDIT_DTTM,
case when CC.CASE_OWN_NM is not null then MAX(CC.REC_DTTM_PST)  end as LST_AGNT_EDIT_DTTM,
case when CC.CASE_STS_CD='Open'  then MIN(CC.REC_DTTM_PST) end as CASE_OPEN_DTTM,
case when CC.CASE_STS_CD in ( 'Closed', 'Auto Closed') then MIN(CC.REC_DTTM_PST) end as CASE_CLSE_OR_AUTO_CLSE_DTTM

--CC.PU_DTTM as LMI_PU_DTTM,
--CC.CLS_DTTM as LMI_CLS_DTTM

FROM EDW_KATAMARI_T.CNTCT_CASE CC
INNER JOIN EDW_KATAMARI_T.CNTCT_CASE_EXTN CCE
ON CC.CNTCT_CASE_APND_KEY = CCE.CNTCT_CASE_APND_KEY
INNER JOIN EDW_STAGE_COMN_SRC.STG_CNTCT_CASE_DELTA  DELTA
on CC.CASE_ID = DELTA.CASE_ID
where CC.CASE_ID='23268760'
    group by 1,2,3,4,5 

当我只使用第1组时,仍然给予非聚合函数必须是group by的一部分。

1 个答案:

答案 0 :(得分:1)

您需要将CASE移动到聚合中:

MIN(CASE WHEN CC.CASE_OWN_NM IS NOT NULL THEN CC.REC_DTTM_PST END) AS FST_AGNT_EDIT_DTTM,
MAX(CASE WHEN CC.CASE_OWN_NM IS NOT NULL THEN CC.REC_DTTM_PST END) AS LST_AGNT_EDIT_DTTM,
MIN(CASE WHEN CC.CASE_STS_CD='Open'  THEN CC.REC_DTTM_PST END) AS CASE_OPEN_DTTM,
MIN(CASE WHEN CC.CASE_STS_CD IN ( 'Closed', 'Auto Closed') THEN CC.REC_DTTM_PST END) AS CASE_CLSE_OR_AUTO_CLSE_DTTM

然后GROUP BY 1将起作用