Mysql sql初学者需要帮助
我对sql和堆栈溢出非常陌生。我希望有人能在这个问题上帮助我。我的查询应该显示销售额超过200000美元的类别的总销售额和总销售额。我已经为这个问题工作了一个小时,现在已经不知所措,非常感谢您的帮助Mysql sql初学者需要帮助,mysql,sql-server,database,Mysql,Sql Server,Database,我对sql和堆栈溢出非常陌生。我希望有人能在这个问题上帮助我。我的查询应该显示销售额超过200000美元的类别的总销售额和总销售额。我已经为这个问题工作了一个小时,现在已经不知所措,非常感谢您的帮助 select distinct c.categoryname, sum(p.unitprice * od.Quantity) as 'Sales' from Categories c inner join Products p on c.CategoryID = p.C
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
选择不同的c.categoryname,
总额(p.单价*od.数量)为“销售额”
来自c类
内连接积
关于c.CategoryID=p.CategoryID
内部连接订单详细信息od
在p.ProductID=od.ProductID上
其中p.单价<200000
按c.categoryname分组
我希望我至少在正确的轨道上,谢谢你的帮助 试试这个版本:
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
您正在筛选单价低于20000的产品,这是为什么?它可能是
sales>200000
而不是unitprice@prashanth-我尝试了您的建议,但收到了以下错误:“将varchar值“sales”转换为int数据类型时,转换失败。”感谢您的尝试!我的错。。它比较了字符串“slaes”
,而不是总和(p.unitprice*od.Quantity)
!因此,您可以使用,sum(p.unitprice*od.Quantity)>200000(p.unitprice*od.Quantity)及其:)而不是p.unitprice,这让我走上了正确的方向,我必须使用sum(p.unitprice*od.Quantity)>200000这一行进行更改,谢谢您的帮助@Burhan@user7701115你可以投票选出对你有帮助的答案!如果你不能投票,也可以标记为答案!