初学者sql学生需要帮助

时间:2017-03-13 04:59:00

标签: mysql sql-server database

我对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

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

1 个答案:

答案 0 :(得分:0)

试试这个版本:

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