select
o.ORDERS_ID,
o.TOTALPRODUCT,
o.TOTALADJUSTMENT,
sum(o.TOTALPRODUCT+o.TOTALADJUSTMENT) as Paid_Total_orders,
p.APPROVEDAMOUNT
from
orders o,
ppcpayinst p
where
o.orders_id=p.order_id
and sum(o.TOTALPRODUCT+o.TOTALADJUSTMENT)!= p.APPROVEDAMOUNT
group by
o.orders_id,
o.TOTALPRODUCT,
o.TOTALADJUSTMENT,
p.APPROVEDAMOUNT"
错误:
SQL0120N无效使用聚合函数或OLAP函数。 SQLSTATE = 42903
有些人可以更正我的查询
答案 0 :(得分:2)
其他答案对于您看到此错误的原因是正确的,但我强烈怀疑您是以错误的方式处理整个查询。
我认为您更有可能只是尝试这样做:
SELECT
o.ORDERS_ID,
o.TOTALPRODUCT,
o.TOTALADJUSTMENT,
o.TOTALPRODUCT+o.TOTALADJUSTMENT as Paid_Total_orders,
p.APPROVEDAMOUNT
FROM
orders o,
ppcpayinst p
WHERE
o.orders_id=p.order_id
and o.TOTALPRODUCT+o.TOTALADJUSTMENT != p.APPROVEDAMOUNT
如果我的怀疑是正确的,那么另一个查询会给你完全相同的结果,但同时做了很多不合逻辑的GROUP
,你只是将单行分组在一起并且SUM
单个值。
我无法想到为什么你需要GROUP BY
来自SELECT
的每个表格的{{1}}并同时使用聚合的原因。
如果我不对,那么我道歉,但我认为情况非常可能。
答案 1 :(得分:1)
您不能在WHERE子句中使用聚合函数,因为它们仅在GROUP BY之后计算。这是HAVING的工作:
select
o.ORDERS_ID,
o.TOTALPRODUCT,
o.TOTALADJUSTMENT,
sum(o.TOTALPRODUCT+o.TOTALADJUSTMENT) as Paid_Total_orders,
p.APPROVEDAMOUNT
from
orders o,
ppcpayinst p
where
o.orders_id=p.order_id
group by
o.orders_id,
o.TOTALPRODUCT,
o.TOTALADJUSTMENT,
p.APPROVEDAMOUNT
HAVING sum(o.TOTALPRODUCT+o.TOTALADJUSTMENT)!= p.APPROVEDAMOUNT
答案 2 :(得分:0)
select
o.ORDERS_ID,
o.TOTALPRODUCT,
o.TOTALADJUSTMENT,
sum(o.TOTALPRODUCT+o.TOTALADJUSTMENT) as Paid_Total_orders,
p.APPROVEDAMOUNT
from orders o INNER JOIN ppcpayinst p
ON o.orders_id=p.order_id
group by
o.orders_id,
o.TOTALPRODUCT,
o.TOTALADJUSTMENT,
p.APPROVEDAMOUNT
HAVING sum(o.TOTALPRODUCT+o.TOTALADJUSTMENT)!= p.APPROVEDAMOUNT
哪里有人
过滤器/ where子句中使用的条件甚至在返回的行上应用任何聚合函数之前过滤行。
让子句在聚合完成后过滤行。