SELECT
p."ID" AS "商品ID",
p."商品名称",
COALESCE(p."库存数量", 0) +
COALESCE(SUM(CASE WHEN pi."商品ID" IS NOT NULL THEN pi."商品数量" ELSE 0 END), 0) -
COALESCE(SUM(CASE WHEN pr."商品ID" IS NOT NULL THEN pr."退货数量" ELSE 0 END), 0) AS "库存数量"
FROM
"商品信息" p
LEFT JOIN
"采购入库子表" pi ON p."ID" = pi."商品ID"
LEFT JOIN
"采购退货子表" pr ON p."ID" = pr."商品ID"
GROUP BY
p."ID", p."商品名称", p."库存数量"; |