如何按 Column1 分组并将 Column2 与 Count(*) 相乘?
这就是我想要实现的目标:
SELECT COUNT(*) * dd.Material_Quantity as [Count], tabSparePart.SparePartName
FROM tabDataDetail AS dd INNER JOIN
tabSparePart ON dd.fiSparePart = tabSparePart.idSparePart
WHERE dd.Reused_Indicator = 1
GROUP BY tabSparePart.SparePartName
ORDER BY Count DESC, tabSparePart.SparePartName
计算所有备件的出现次数并将它们与其相应的Material_Quantity值相乘。 但我收到以下错误:
8118:列“tabDataDetail.Material_Quantity”在选择列表中无效,因为它未包含在聚合函数中并且没有 GROUP BY 条款。
我怎样才能有意义地将两个值相乘?简单的 SUM
或 AVG
会给出正确的结果吗?例如:COUNT(*) * AVG(dd.Material_Quantity) as [Count]
this is what i'm trying to achieve:
SELECT COUNT(*) * dd.Material_Quantity as [Count], tabSparePart.SparePartName
FROM tabDataDetail AS dd INNER JOIN
tabSparePart ON dd.fiSparePart = tabSparePart.idSparePart
WHERE dd.Reused_Indicator = 1
GROUP BY tabSparePart.SparePartName
ORDER BY Count DESC, tabSparePart.SparePartName
Count all sparepart occurences and multiply them with their corresponding Material_Quantity
value.
But i get following error:
8118: Column 'tabDataDetail.Material_Quantity' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY
clause.
How can i multiply both values meaningfully? Would a simple SUM
or AVG
give the correct result? For example: COUNT(*) * AVG(dd.Material_Quantity) as [Count]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要查找数量总和,可以使用
sum
:If you're looking for the sum of quantities, you could use
sum
: