无法弄清楚这个teradata脚本中的错误

时间:2012-04-11 20:21:46

标签: sql oracle teradata

脚本如下:

SELECT
   CAST(SUBSTR(TRIM(ABU_PTY_CD),4,2) AS CHAR(2)) ABU_PTY_CD,
   ACCTG_PRD_YR_CD,
   ACCTG_PRD_MO_CD ,

   CASE 
       WHEN (OUDS_FAC_CD > ' ' ) THEN CAST (SUBSTR(OUDS_FAC_CD, 1, 5) AS CHAR(5))
       ELSE CAST (COALESCE(CC_PTY_CD, '') AS CHAR(5)) 
   END OUDS_FAC_CD,

   CASE 
       WHEN GL_ACCT_NUM > ' ' THEN CAST (SUBSTR(GL_ACCT_NUM, 1, 12) AS CHAR(12)) 
       ELSE CAST (SAP_ACCT_NUM AS CHAR(12)) 
   END ACCT_NUM,

   CASE
       WHEN LE_PTY_CD > ' ' THEN CAST (LE_PTY_CD AS CHAR(4) )
       ELSE CAST (SUBSTR( COMP_PTY_CD, 1, 4) AS CHAR(4) )
   END COMP_PTY_CD,

   JE_LWR_TIER_RPTG_NUM, 
   LT2_NM, 

   CASE
       WHEN PRD_VAR_CD > ' ' THEN CAST (PRD_VAR_CD AS CHAR(3))
       ELSE CAST (COALESCE(FA_PTY_CD, '') AS CHAR(3))  
   END FA_PTY_CD,

   JE_LN_GLBL_CCY_CD, 
   JE_LN_LC_CD,

   CASE 
       WHEN GL_ACCT_LVL_NUM > ' ' THEN CAST(SUBSTR(GL_ACCT_LVL_NUM,1,10)  AS CHAR(11) ) 
       ELSE CAST(SUBSTR(FNCL_STMT_LN_NUM,1,11) AS CHAR(11) )
   END ACCT_LVL_NUM,

   SUM (JE_LN_GLBL_AMT) JE_LN_GLBL_AMT, 
   DDU_FAC_CD, 
   JE_HDR_DESC, 
   JE_HDR_NUM, 
   JE_GRP_NUM, 
   SOURCE_SYSTEM_ID
FROM   EDW_BI_SL_M1.BFV 
WHERE  CPC_PTY_CD = '00000000SX'
       AND TO_CHAR(RPTG_ACCTG_DT, 'YYYY')  >= TO_CHAR(ADD_MONTHS((CURRENT_DATE),-1),'YYYY')
       AND JE_VERS_NUM in ('200', '001') 
       AND SRC_LDGR_CD <> 'LG'
       AND ACCTG_PRD_MO_CD < 13
GROUP BY ABU_PTY_CD, ACCTG_PRD_YR_CD, ACCTG_PRD_MO_CD, OUDS_FAC_CD, ACCT_NUM, COMP_PTY_CD, JE_LWR_TIER_RPTG_NUM, 
LT2_NM, FA_PTY_CD, JE_LN_GLBL_CCY_CD, JE_LN_LC_CD, ACCT_LVL_NUM, JE_LN_GLBL_AMT, DDU_FAC_CD, JE_HDR_DESC, JE_HDR_NUM, JE_GRP_NUM, 
SOURCE_SYSTEM_ID;

错误讯息:

3504:所选的非聚合值必须是关联组的一部分

2 个答案:

答案 0 :(得分:0)

您已在JE_LN_GLBL_AMT子句中添加了GROUP BY列,但这是一个求和值。

答案 1 :(得分:0)

您不能在同一查询的GROUP BY子句中使用您在此查询中创建的别名(至少在Oracle中)。您必须使用表达式:

...
GROUP BY
CAST(SUBSTR(TRIM(ABU_PTY_CD),4,2) AS CHAR(2)), 
..., 
CASE 
   WHEN GL_ACCT_LVL_NUM > ' ' THEN CAST(SUBSTR(GL_ACCT_LVL_NUM,1,10)  AS CHAR(11) ) 
   ELSE CAST(SUBSTR(FNCL_STMT_LN_NUM,1,11) AS CHAR(11) )
END, 
..., 
SOURCE_SYSTEM_ID

或创建子查询,然后使用别名进行分组:

SELECT ABU_PTY_CD, ...
FROM (
SELECT
   CAST(SUBSTR(TRIM(ABU_PTY_CD),4,2) AS CHAR(2)) ABU_PTY_CD,
   ...
FROM   EDW_BI_SL_M1.BFV 
WHERE  CPC_PTY_CD = '00000000SX'
       AND TO_CHAR(RPTG_ACCTG_DT, 'YYYY')  >= TO_CHAR(ADD_MONTHS((CURRENT_DATE),-1),'YYYY')
       AND JE_VERS_NUM in ('200', '001') 
       AND SRC_LDGR_CD <> 'LG'
       AND ACCTG_PRD_MO_CD < 13
)
GROUP BY ABU_PTY_CD, ACCTG_PRD_YR_CD, ACCTG_PRD_MO_CD, OUDS_FAC_CD, ACCT_NUM, COMP_PTY_CD, JE_LWR_TIER_RPTG_NUM, 
LT2_NM, FA_PTY_CD, JE_LN_GLBL_CCY_CD, JE_LN_LC_CD, ACCT_LVL_NUM, JE_LN_GLBL_AMT, DDU_FAC_CD, JE_HDR_DESC, JE_HDR_NUM, JE_GRP_NUM, 
SOURCE_SYSTEM_ID;