2017-10-07 43 views
-1

我工作的家庭作業,這是一個問題:錯誤在SQL集團通過

  • 創建從出貨量,ShipItems和品牌表返回行的視圖名爲ShipItemsBrands。

  • 該視圖應該從貨物表中返回這些列:ShipmentID,ShipmentOrderDate,TaxAmount和ShipDate。

  • 此視圖應從ShipItems表中返回這些列:ShipItemPrice,ShipItemDiscountAmount,FinalPrice(從項目價格減去折扣金額),Quantity和ItemTotal(項目的計算總數)。

  • 此視圖應返回品牌表中的BrandName列。

這裏是我的代碼:

CREATE VIEW ShipItemsBrand 
AS 
    SELECT 
     sh.ShipmentID, sh.ShipmentOrderDate, sh.TaxAmount, sh.ShipDate, 
     si.ShipItemPrice, si.ShipItemDiscountAmount, 
     SUM(si.ShipItemPrice - si.ShipItemDiscountAmount) As FinalPrice, 
     si.Quantity, 
     SUM((si.ShipItemPrice - si.ShipItemDiscountAmount) * si.Quantity) AS ItemTotal, 
     b.BrandName 
    FROM 
     Brands AS b 
    JOIN 
     ShipItems AS si ON b.BrandID = si.BrandID 
    JOIN 
     Shipments AS sh ON si.ShipmentID = sh.ShipmentID 
    GROUP BY 
     sh.ShipmentID 

我收到錯誤代碼:

,因爲它不包含在列「Shipments.ShipmentOrderDate」在選擇列表中無效集合函數或GROUP BY子句。

是否需要將sh.ShipOrderDate添加到GROUP BY?如果是這樣,爲什麼呢?

回答

1

錯誤代碼:因爲它不是在聚合 函數或GROUP BY子句中包含列「Shipments.ShipmentOrderDate」是 選擇列表中無效。

我是否需要將sh.ShipOrderDate添加到GROUP BY?如果是這樣,爲什麼是 呢?

排序答案是:

這是您的SELECT子句(格式化)。這些列已被分成兩個部分1.「非聚合」列和2。的「聚集」列,其是thse使用聚合功能等SUM/COUNT/MIN/MAX/AVG等

SELECT 
    -- "non-aggregating columns" 
    sh.ShipmentID, sh.ShipmentOrderDate, sh.TaxAmount 
, sh.ShipDate, si.ShipItemPrice, si.ShipItemDiscountAmount 
, si.Quantity, b.BrandName 
-- "aggregating columns" 
, SUM(si.ShipItemPrice - si.ShipItemDiscountAmount) As FinalPrice 
, SUM((si.ShipItemPrice - si.ShipItemDiscountAmount) * si.Quantity) AS ItemTotal 

立即重新讀取錯誤消息:because it is not contained in either an aggregate function or the GROUP BY clause.這意味着:

  1. 從SELECT子句中刪除「非聚集」列或
  2. 添加「非聚集」列到GROUP BY子句

最終你會發現,所有「不正聚集」列必須在GROUP上市BY子句,像這樣:

CREATE VIEW ShipItemsBrand AS 
SELECT 
     -- "non-aggregating columns" 
     sh.ShipmentID, sh.ShipmentOrderDate, sh.TaxAmount 
    , sh.ShipDate, si.ShipItemPrice, si.ShipItemDiscountAmount 
    , si.Quantity, b.BrandName 
    -- "aggregating columns" 
    , SUM(si.ShipItemPrice - si.ShipItemDiscountAmount) As FinalPrice 
    , SUM((si.ShipItemPrice - si.ShipItemDiscountAmount) * si.Quantity) AS ItemTotal 
FROM Brands AS b 
    JOIN ShipItems AS si ON b.BrandID = si.BrandID 
    JOIN Shipments AS sh ON si.ShipmentID = sh.ShipmentID 
GROUP BY 
     -- "non-aggregating columns" 
     sh.ShipmentID, sh.ShipmentOrderDate, sh.TaxAmount 
    , sh.ShipDate, si.ShipItemPrice, si.ShipItemDiscountAmount 
    , si.Quantity, b.BrandName 

提示:整理SELECT子句輸入到2個部分,然後複製/粘貼下組非聚集列通過。 nb:在粘貼之後刪除任何列別名

注意:在舊版本的MySQL中,可以在group by子句中列出較少的非聚合列。但是這是MySQL特有的,它不被SQL標準支持。另外最近的MySQL版本已經改變了默認行爲,並且它也變得更符合標準。

+0

沒問題。 *(ps:upvoting也是一種表達感謝的方式,「接受」表示你已經選擇了一種解決方案,這有助於他人理解什麼對你有幫助,同時他們也在尋找類似的答案。)* https://stackoverflow.com/help/someone-answers –

+0

我upvoted,但沒有代表它顯示 –

+0

代表哦,好的,謝謝。 NB:只有你可以「接受」,而不依賴於代表。 –

1

您必須在組中按條款包含'Shipments.ShipmentOrderDate'。 有必要在Group By子句中包含非操作列。

CREATE VIEW ShipItemsBrand AS 
SELECT sh.ShipmentID, sh.ShipmentOrderDate, sh.TaxAmount, sh.ShipDate, 
si.ShipItemPrice, si.ShipItemDiscountAmount, SUM(si.ShipItemPrice - si.ShipItemDiscountAmount) As FinalPrice, si.Quantity, SUM((si.ShipItemPrice - si.ShipItemDiscountAmount) * si.Quantity) AS ItemTotal, 
b.BrandName 
FROM Brands AS b 
    JOIN ShipItems AS si 
     ON b.BrandID = si.BrandID 
    JOIN Shipments AS sh 
     ON si.ShipmentID = sh.ShipmentID 
GROUP BY sh.ShipmentID,sh.ShipmentOrderDate 
+0

那麼我還必須將sh.TaxAmount和sh.ShipDate添加到Group by子句中,然後才能正確嗎?我將這些添加到子句的順序是否會影響查詢的結果? –

0
CREATE VIEW ShipItemsBrand AS 
SELECT sh.ShipmentID,sh.TaxAmount, sh.ShipDate, 
si.ShipItemPrice, si.ShipItemDiscountAmount, SUM(si.ShipItemPrice - 
si.ShipItemDiscountAmount) As FinalPrice, si.Quantity, SUM((si.ShipItemPrice 
- si.ShipItemDiscountAmount) * si.Quantity) AS ItemTotal, 
b.BrandName 
FROM Brands AS b 
JOIN ShipItems AS si 
    ON b.BrandID = si.BrandID 
JOIN Shipments AS sh 
    ON si.ShipmentID = sh.ShipmentID 
GROUP BY sh.ShipmentID 

CREATE VIEW NewShipItemsBrand AS 
SELECT * from ShipItemsBrand left join Shipments on si.ShipmentID = 
sh.ShipmentID and sh.ShipmentOrderDate=si.ShipmentID