查看返回部分或不正确数据集的总和

时间:2013-01-02 23:56:12

标签: mysql

我有以下观点。我需要查看每个日期的每个PartNum的总和。但视图返回的错误数据集仅显示某些日期。有人可以让我直截了当吗?

SELECT
    `archived_items`.`ArcPartNumber` AS `PartNum`,
    `archived_header`.`AhDeliveryDate` AS `DeliveryDate`,
    `archived_items`.`ArcTransactionNumber` AS `TransactionNumber`,
    sum(`archived_items`.`ArcOrdered`) AS `Ordered`,
    sum(`archived_items`.`ArcShipped`) AS `Shipped`,
    sum(`archived_items`.`ArcBackOrdered`) AS `BackOrdered`
FROM
    ( `archived_items`
        LEFT JOIN `archived_header` ON (
            (`archived_header`.`AhTransactionNumber` = `archived_items`.`ArcTransactionNumber`
        )))
WHERE
((`archived_items`.`ArcTransactionType` = 3)
    AND 
    (`archived_header`.`AhDeliveryDate` >= CURDATE()))
GROUP BY
`archived_items`.`ArcPartNumber`
`archived_header`.`AhDeliveryDate`,
ORDER BY
`archived_items`.`ArcPartNumber`,
`archived_header`.`AhDeliveryDate`

2 个答案:

答案 0 :(得分:0)

试试这个。你在不需要的地方使用了很多()

 SELECT
       ar.`ArcPartNumber` AS `PartNum`,
       ah.`AhDeliveryDate` AS `DeliveryDate`,
       ar.`ArcTransactionNumber` AS `TransactionNumber`,
       sum(ar.`ArcOrdered`) AS `Ordered`,
       sum(ar.`ArcShipped`) AS `Shipped`,
       sum(ar.`ArcBackOrdered`) AS `BackOrdered`
 FROM   `archived_items` ar
 LEFT JOIN `archived_header` ah 
 ON    ah.`AhTransactionNumber` = ar.`ArcTransactionNumber`
 WHERE
       ar.`ArcTransactionType` = 3
 AND 
      ah.`AhDeliveryDate` >= CURDATE()   
 GROUP BY
       `PartNum`,
       `DeliveryDate`
 ORDER BY
       `PartNum`,
       `DeliveryDate`

//不要使用CURDATE因为它已经是一个有效期的函数。我用ah.AhDeliveryDate替换了它,所以请确保它们的格式相同

look demo它运作得很好。

答案 1 :(得分:0)

看起来你只是按照你应该的两个列进行分组。不确定这是一个错字还是问题的原因。试试这个

SELECT
    `archived_items`.`ArcPartNumber` AS `PartNum`,
    `archived_header`.`AhDeliveryDate` AS `DeliveryDate`,
    `archived_items`.`ArcTransactionNumber` AS `TransactionNumber`,
    sum(`archived_items`.`ArcOrdered`) AS `Ordered`,
    sum(`archived_items`.`ArcShipped`) AS `Shipped`,
    sum(`archived_items`.`ArcBackOrdered`) AS `BackOrdered`
FROM
    ( `archived_items`
        LEFT JOIN `archived_header` ON (
            (`archived_header`.`AhTransactionNumber` = `archived_items`.`ArcTransactionNumber`
        )))
WHERE
((`archived_items`.`ArcTransactionType` = 3)
    AND 
    (`archived_header`.`AhDeliveryDate` >= CURDATE()))
GROUP BY
`archived_items`.`ArcPartNumber`
`archived_header`.`AhDeliveryDate`,
`archived_items`.`ArcTransactionNumber`
ORDER BY
`archived_items`.`ArcPartNumber`,
`archived_header`.`AhDeliveryDate`