2013-01-02 53 views
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` 

回答

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` 
+0

感謝您的嘗試。相同的結果。這真的很奇怪。 – AlphaBase

+0

這不是視圖。還有其他的錯誤。再次感謝你的幫助。 – AlphaBase

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 beecause它已經CURENT日期功能。我把它替換成你的ah.AhDeliveryDate,所以確保它們是相同的格式

look demo它工作得很好。

+0

更新了我的答案 –

+0

感謝您的迴應。這個額外的CURDATE是我的一個錯字。所以你的觀點是我的觀點,因此我仍然得到一個部分數據集。請注意,所有這些括號都是由我正在使用的Navicat生成的。 – AlphaBase

+0

編輯我的代碼,這是一些錯誤的逗號,現在嘗試上面的代碼,並看到演示 –