甲骨文求和
Oracle Having Sum
我无法使“求和”正常工作。我不确定 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
你应该 post 你得到的确切错误,我在没有它的情况下做出假设..
你需要一个 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
试试看是否效果更好,如果不行,请post完整准确的错误信息
SUM
不支持多表达式。
- 子句的顺序为:
WITH ... SELECT ... FROM ... WHERE ... GROUP BY ... HAVING ... ORDER BY ...
。
- 如果有非聚合列,为了使用
HAVING
子句,您需要一个 GROUP BY
子句。
- 您不能在
WHERE
或 HAVING
子句中使用 SELECT
子句中定义的列别名。
类似于:
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;
我无法使“求和”正常工作。我不确定 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
你应该 post 你得到的确切错误,我在没有它的情况下做出假设..
你需要一个 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
试试看是否效果更好,如果不行,请post完整准确的错误信息
SUM
不支持多表达式。- 子句的顺序为:
WITH ... SELECT ... FROM ... WHERE ... GROUP BY ... HAVING ... ORDER BY ...
。 - 如果有非聚合列,为了使用
HAVING
子句,您需要一个GROUP BY
子句。 - 您不能在
WHERE
或HAVING
子句中使用SELECT
子句中定义的列别名。
类似于:
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;