我在使用SQL添加和减去来自不同表的值时遇到问题,添加和减去工作正常,但它返回INCORRECT值
这是我的查询,从表1(DELIVERIES表)中添加“QUANTITY”,按“CONTAINER_TYPE”分组,与表2(PULLOUT表)相同,我想要第一个表的数量总和为被减去第二个表的数量之和。但我得到并且不正确的数据。
SELECT
CUSTOMER.CUSTOMER_NAME,
DELIVERIES.CONTAINER_TYPE,
COALESCE(SUM(DELIVERIES.QUANTITY), 0) AS TOTAL_DELIVERY,
COALESCE(SUM(PULLOUTS.QUANTITY), 0) AS TOTAL_RETURNS,
COALESCE(SUM(DELIVERIES.QUANTITY), 0) - COALESCE(SUM(PULLOUTS.QUANTITY), 0) AS CUSTOMER_BALANCE
FROM EJPADMIN.CUSTOMER
INNER JOIN DELIVERIES
ON DELIVERIES.CUSTOMER_ID = CUSTOMER.CUSTOMER_ID
LEFT JOIN PULLOUTS
ON PULLOUTS.DELIVERY_ID = DELIVERIES.DELIVERY_ID
GROUP BY CUSTOMER.CUSTOMER_NAME, DELIVERIES.CONTAINER_TYPE
伙计们请帮忙。三江源!
DELIVERIES表 PULLOUT表 CUSTOMER TRANSACTION表(不正确的结果)