我有以下查询。它使用三个子查询并且经常执行,所以我想将查询重写为JOIN
. 尽管我的值不正确,但我尝试这样做,有谁知道我如何成功地重写查询?
使用子查询进行查询
SELECT PRODUCT.ID
FROM PRODUCT WHERE (
COALESCE((SELECT SUM(AMOUNT) FROM CORRECTION WHERE CORRECTION.PRODUCT_ID = PRODUCT.ID),0)
- COALESCE((SELECT SUM(AMOUNT) FROM ORDERROW WHERE ORDERROW.PRODUCTID = PRODUCT.ID),0)
+ COALESCE((SELECT SUM(AMOUNTOFPACKAGES * AMOUNTPERPACKAGE) FROM DELIVERYROW WHERE DELIVERYROW.PRODUCTID = PRODUCT.ID),0)
) > 0
带有连接的错误查询
SELECT PRODUCT.ID
FROM PRODUCT
LEFT JOIN CORRECTION ON CORRECTION.PRODUCT_ID = PRODUCT.ID
LEFT JOIN ORDERROW ON ORDERROW.PRODUCTID = PRODUCT.ID
LEFT JOIN DELIVERYROW ON DELIVERYROW.PRODUCTID = PRODUCT.ID
GROUP BY PRODUCT.ID
HAVING (COALESCE(SUM(CORRECTION.AMOUNT),0)
- COALESCE(SUM(ORDERROW.AMOUNT),0)
+ COALESCE(SUM(DELIVERYROW.AMOUNTOFPACKAGES * DELIVERYROW.AMOUNTPERPACKAGE),0)
) > 0