2017-10-07 76 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