【问题标题】:Beginner sql student in need of assistance需要帮助的初级sql学生
【发布时间】:2017-03-13 04:59:00
【问题描述】:

我对 sql 和 Stack Overflow 非常陌生。我希望有人可以帮助我进行此查询。我的查询应该显示销售额超过 200,000 美元的类别的总销售额和总销售额。我已经在这个查询上工作了一个小时,而且我束手无策,感谢您的帮助!

   select distinct c.categoryname,
   sum(p.unitprice * od.Quantity) as 'Sales'
   from Categories c
   inner join Products p
   on c.CategoryID = p.CategoryID
   inner join OrderDetails od
   on p.ProductID = od.ProductID
   where p.unitprice < 200000 
   group by c.categoryname

我希望我至少在正确的轨道上,感谢您的帮助!

【问题讨论】:

  • 您正在过滤那些单价小于 20000的产品,这是为什么呢?
  • 可能是sales &gt; 200000 而不是单价
  • @prashanth - 我尝试了您的建议,但收到此错误:“将 varchar 值 'sales' 转换为数据类型 int 时转换失败。”无论如何感谢您的尝试!
  • 我的错......它比较了字符串"slaes"而不是sum(p.unitprice * od.Quantity)!因此,您可以使用 sum(p.unitprice * od.Quantity) &gt; 200000 及其 &gt; 来代替 p.unitprice :)

标签: mysql sql-server database


【解决方案1】:

试试这个版本:

SELECT
   c.categoryname,
   sum(od.Quantity) as "Items Sold",  -- you were missing this
   sum(p.unitprice * od.Quantity) as "Sales"
FROM
   Categories c
   INNER JOIN Product p ON c.CategoryID = p.CategoryID,
   INNER JOIN OrderDetails od on p.ProductID = od.ProductID
WHERE
   sum(p.unitprice * od.Quantity) > 200000  -- filter on the sales, not product
GROUP BY
   c.categoryname

【讨论】:

  • 这让我朝着正确的方向前进,我必须通过使用这条总和 (p.UnitPrice * od.Quantity) > 200000 的行来进行更改,谢谢您的帮助! @Burhan
  • @user7701115 您可能会赞成对您有帮助的答案!如果您不能投票,请标记为答案!
猜你喜欢
  • 2021-04-11
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多