0

下面的视图没有返回正确的结果。我正在尝试获取按 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`
4

2 回答 2

0

您需要检查PtPart条款ON。否则,您将不会在子表中获得不匹配的行,因为这些列将是NULL.

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`
    AND `printed`.`PtPart` = 'Barcode Print'
LEFT JOIN `product_trans` `scanned`
ON `scanned`.`PtPlanId` = `prod_plan`.`Prp_ProdPlanId`
    AND `scanned`.`PtPartNum` = `prod_plan`.`Prp_PartNum`
    AND `scanned`.`PtPart` = 'Barcode Scan'
GROUP BY
    `prod_plan`.`Prp_ProdPlanId`,
    `prod_plan`.`Prp_PartNum`
于 2013-02-26T01:01:04.777 回答
0

这是我所做的。以下是正常工作的 3 个视图。

v_trans_printed:

SELECT
product_trans.PtPlanId AS PlanId,
product_trans.PtLot AS Lot,
product_trans.PtPartNum AS PartNum,
Sum(product_trans.PtQty) AS Printed
FROM
product_trans
WHERE
product_trans.PtPart = 'Barcode Print'
GROUP BY
product_trans.PtPlanId,
product_trans.PtLot,
product_trans.PtPartNum

v_trans_scanned:

SELECT
product_trans.PtPlanId AS PlanId,
product_trans.PtLot AS Lot,
product_trans.PtPartNum AS PartNum,
Sum(product_trans.PtQty) AS Scanned
FROM
product_trans
WHERE
product_trans.PtPart = 'Barcode Scan'
GROUP BY
product_trans.PtPlanId,
product_trans.PtLot,
product_trans.PtPartNum

在这里,我把它们放在一起。这将返回正确的结果:

vSELECT
prod_plan.Prp_ProdPlanId AS PlanId,
prod_plan.Prp_Lot AS Lot,
prod_plan.Prp_PartNum AS PartNum,
Sum(prod_plan.Prp_Picked) AS Picked,
Printed.Printed AS Printed,
Scanned.Scanned AS Scanned
FROM
prod_plan
LEFT JOIN v_trans_printed AS Printed ON Printed.PlanId = prod_plan.Prp_ProdPlanId AND Printed.Lot = prod_plan.Prp_Lot AND Printed.PartNum = prod_plan.Prp_PartNum
LEFT JOIN v_trans_scanned AS Scanned ON Scanned.PlanId = prod_plan.Prp_ProdPlanId AND Scanned.Lot = prod_plan.Prp_Lot AND Scanned.PartNum = prod_plan.Prp_PartNum
GROUP BY
prod_plan.Prp_ProdPlanId,
prod_plan.Prp_Lot,
prod_plan.Prp_PartNum

但如果我可以使用一种视图会更好。

注意:我最初无意中遗漏了Lot。但这并不影响结果w。样本数据集。

于 2013-02-26T15:44:34.857 回答