下面的视图没有返回正确的结果。我正在尝试获取按 Plan_Id 和 PartNum 分组的 Picked、Printed 和 Scanned 的总和。无论子表中是否有相应的记录,我都需要返回正确的总数。如果我使用三个不同的视图并加入它们,我知道该怎么做,但是我如何在一个视图中完成所有操作?任何帮助表示赞赏。
SELECT
`prod_plan`.`Prp_ProdPlanId` AS `PlanId`,
`prod_plan`.`Prp_PartNum` AS `PartNum`,
sum(`prod_plan`.`Prp_Picked`) AS `Picked`,
sum(`printed`.`PtQty`) AS `Printed`,
sum(`scanned`.`PtQty`) AS `Scanned`
FROM
(
(
`prod_plan`
LEFT JOIN `product_trans` `printed` ON (
(
(
`printed`.`PtPlanId` = `prod_plan`.`Prp_ProdPlanId`
)
AND (
`printed`.`PtPartNum` = `prod_plan`.`Prp_PartNum`
)
)
)
)
LEFT JOIN `product_trans` `scanned` ON (
(
(
`scanned`.`PtPlanId` = `prod_plan`.`Prp_ProdPlanId`
)
AND (
`scanned`.`PtPartNum` = `prod_plan`.`Prp_PartNum`
)
)
)
)
WHERE
(
(
`printed`.`PtPart` = 'Barcode Print'
)
AND (
`scanned`.`PtPart` = 'Barcode Scan'
)
)
GROUP BY
`prod_plan`.`Prp_ProdPlanId`,
`prod_plan`.`Prp_PartNum`