我无法让这笔工作得到满足。我不确定select语句中的Sum是否适用于我在Oracle SQL上的新手。我试图找出应付金额大于客户信用额度的位置。
SELECT ARCUSTO.CUSTNO,
ARCUSTO.COMPANY,
ARCUSTO.ID,
ARCUSTO.FAX_NUMBER,
ARCUSTO.STATUS_ID,
ARCUSTO.CREDIT_LIMIT,
ARCUSTO.ADDR1,
ARCUSTO.ADDR2,
ARCUSTO.ADDR3,
ARCUSTO.CITY,
ARCUSTO.STATE,
ARCUSTO.ZIP,
ARCUSTO.COUNTRY,
ARCUSTO.PHONE_NUMBER,
SUM(V_1_30_SUM.LEFT_TO_APPLY, V_30_60_SUM.LEFT_TO_APPLY, V_60_90_SUM.LEFT_TO_APPLY, V_90_SUM.LEFT_TO_APPLY, V_CURRENT_SUM.LEFT_TO_APPLY) Due
FROM ( ( ( (IQMS.ARCUSTO ARCUSTO
LEFT OUTER JOIN IQMS.V_1_30_SUM V_1_30_SUM
ON ARCUSTO.ID=V_1_30_SUM.ARCUSTO_ID)
LEFT OUTER JOIN IQMS.V_30_60_SUM V_30_60_SUM
ON ARCUSTO.ID=V_30_60_SUM.ARCUSTO_ID)
LEFT OUTER JOIN IQMS.V_60_90_SUM V_60_90_SUM
ON ARCUSTO.ID=V_60_90_SUM.ARCUSTO_ID)
LEFT OUTER JOIN IQMS.V_90_SUM V_90_SUM
ON ARCUSTO.ID=V_90_SUM.ARCUSTO_ID)
LEFT OUTER JOIN IQMS.V_CURRENT_SUM V_CURRENT_SUM
ON ARCUSTO.ID=V_CURRENT_SUM.ARCUSTO_ID
ORDER BY
ARCUSTO.CUSTNO,
ARCUSTO.ID,
ARCUSTO.COMPANY
HAVING SUM(Due) > ARCUST.CREDIT_LIMIT
答案 0 :(得分:1)
SUM
不支持多个表达式。WITH ... SELECT ... FROM ... WHERE ... GROUP BY ... HAVING ... ORDER BY ...
。HAVING
子句,如果存在非聚合列,则需要GROUP BY
子句。SELECT
或WHERE
子句中HAVING
子句中定义的列别名。类似的东西:
SELECT *
FROM (
SELECT A.*,
COALESCE( ( SELECT SUM( LEFT_TO_APPLY ) FROM V_1_30_SUM WHERE A.ID = ARCUSTO_ID ), 0 )
+ COALESCE( ( SELECT SUM( LEFT_TO_APPLY ) FROM V_30_60_SUM WHERE A.ID = ARCUSTO_ID ), 0 )
+ COALESCE( ( SELECT SUM( LEFT_TO_APPLY ) FROM V_60_90_SUM WHERE A.ID = ARCUSTO_ID ), 0 )
+ COALESCE( ( SELECT SUM( LEFT_TO_APPLY ) FROM V_90_SUM WHERE A.ID = ARCUSTO_ID ), 0 )
+ COALESCE( ( SELECT SUM( LEFT_TO_APPLY ) FROM V_CURRENT_SUM WHERE A.ID = ARCUSTO_ID ), 0 ) AS Due
FROM IQMS.ARCUSTO A
ORDER BY
A.CUSTNO,
A.ID,
A.COMPANY
)
WHERE Due > Credit_Limit;
答案 1 :(得分:0)
你应该发布你得到的确切错误,我没有它做出假设..
您需要GROUP BY
...并且您的HAVING
无法引用列别名..您需要重新声明完整的SUM
..
ORDER BY
排在最后。
所以你的最终作品应该是这样的:
GROUP BY ARCUSTO.CUSTNO,
ARCUSTO.COMPANY,
ARCUSTO.ID,
ARCUSTO.FAX_NUMBER,
ARCUSTO.STATUS_ID,
ARCUSTO.CREDIT_LIMIT,
ARCUSTO.ADDR1,
ARCUSTO.ADDR2,
ARCUSTO.ADDR3,
ARCUSTO.CITY,
ARCUSTO.STATE,
ARCUSTO.ZIP,
ARCUSTO.COUNTRY,
ARCUSTO.PHONE_NUMBER,
HAVING SUM(V_1_30_SUM.LEFT_TO_APPLY, V_30_60_SUM.LEFT_TO_APPLY, V_60_90_SUM.LEFT_TO_APPLY, V_90_SUM.LEFT_TO_APPLY, V_CURRENT_SUM.LEFT_TO_APPLY)
> ARCUST.CREDIT_LIMIT
ORDER BY
ARCUSTO.CUSTNO,
ARCUSTO.ID,
ARCUSTO.COMPANY
试一试,看看它是否有效,如果没有,请发布完全准确的错误消息