DB2 - 聚合函数的使用无效SQLCODE = -120

时间:2017-02-21 09:59:56

标签: sql db2 ibm-data-studio

任何人都可以帮我理解这个Db2查询语法有什么问题,它是否因SQLCODE -120而失败,因为我试图使用row_number()?

     SELECT COUNT(ORDER_ID) OVER() TOTAL_NO_OF_RECORDS,
        ROW_NUMBER() OVER (ORDER BY CREATED_DATE DESC) AS ROW_NUM
        FROM (
        SELECT DISTINCT A.ORDER_ID ORDER_ID,
        B.AgencyName  AS AGENCY_NAME,
        C.FirstName FIRST_NAME,
        C.LastName LAST_NAME,
        DEMOGRAPHIC.State STATE,
        A.CreatedTS CREATED_DATE,
        E.WritingTIN WRITING_TIN,
        E.ParentTIN PARENT_TIN
        FROM DBO.TABLE1 A
        INNER JOIN 
        DBO.TABLE2 TABLE2 ON TABLE2.ORDER_ID=A.ORDER_ID
        INNER JOIN 
        DBO.TABLE3 TABLE3 ON TABLE3.QuoteId=TABLE2.QuoteId 
        INNER JOIN 
        DBO.Demographic DEMOGRAPHIC ON  
DEMOGRAPHIC.DemographicId=TABLE3 .DemographicId 
        INNER JOIN 
        DBO.Agent E ON E.AgentId=DEMOGRAPHIC.AgentId
        INNER JOIN 
        DBO.User USER ON USER.WebAcctID=AGENT.WebAcctId
        INNER JOIN 
        DBO.Shopper SHOPPER ON SHOPPER.ShopperId=DEMOGRAPHIC.ShopperId
        LEFT OUTER JOIN
        DBO.Subsidy D ON D.demographicId=DEMOGRAPHIC.demographicId
        LEFT OUTER JOIN
        DBO.Employer EMPLOYER ON DEMOGRAPHIC.demographicId=EMPLOYER.demographicId
        WHERE E.WritingTIN = 'XYZ' AND E.ParentTIN = 'XYZ'
        AND DEMOGRAPHIC.State='OH'
        AND A.Status IN ('X','Y','Z')
        )AS ORDER_DETAILS
        where ROW_NUMBER() OVER (ORDER BY CREATED_DATE DESC) BETWEEN ((1*50)-50)+1 AND 1*50 ORDER BY CREATED_DATE DESC

错误显示:聚合函数或OLAP函数的使用无效.. SQLCODE = -120,SQLSTATE = 42903,DRIVER = 4.18.60

1 个答案:

答案 0 :(得分:0)

您有多个错误:

  • X未在子查询中定义。
  • B.CREATED_DATE在外部查询中引用,但未定义。
  • ROW_NUMBER()用于WHERE子句。

这似乎是您编写的查询的意图:

SELECT COUNT(*) OVER () NO_OF_RECORDS, ROW_NUM
FROM (SELECT A.ID, B.FirstName as FIRST_NAME, B.LastName as LAST_NAME,
             MAX(B.CREATED_DATE) as CREATED_DATE,
             ROW_NUMBER() OVER (ORDER BY MAX(CREATED_DATE) DESC) AS ROW_NUM
      FROM SCHEMANAME.A A INNER JOIN 
           SCHEMANAME.B B
           ON B.ShopperId = A.ShopperId LEFT OUTER JOIN
           SCHEMANAME.C C
           ON C.demographicId = B.demographicId
      WHERE A.WritingTIN = 'XYZ' AND A.ParentTIN = 'XYZ' AND
            B.State = 'OH' AND C.Status IN ('X', 'Y', 'Z')
      GROUP BY A.ID, B.FirstName, B.LastName
     ) ORDER_DETAILS
WHERE ROW_NUM BETWEEN ((1*50)-50)+1 AND 1*50 
ORDER BY CREATED_DATE DESC;

我不确定这个结果是否合理,但它应该可以解决您的错误。