0

我有以下看法。我需要查看每个日期的每个 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`
4

2 回答 2

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,所以要确保它们是相同的格式

看看演示它工作得很好。

于 2013-01-03T00:31:37.597 回答
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`
于 2013-01-03T01:22:31.000 回答