列没有包含在聚合函数或GROUP BY子句中

3

我有两个表格:

DeviceInstaceDevice

它们通过 Foreign Key 连接,即 DeviceInstance.DeviceId=Device.Id

我有一个 SQL 查询:

select d.CatalogNo,d.Manufacturer,d.Name, sum(quantity) 
from DeviceInstance di 
full join Device d 
on d.Id=di.DeviceId 
group by di.DeviceId

我需要做一个小总结,其中包括以下内容:
  • 目录号
  • 制造商
  • 设备名称
  • 设备数量(这些设备ID的数量列值之和)
但我遇到了一些“按组分组”的问题。
我尝试的所有方法都会给我返回像这样的错误信息:
Column 'Device.CatalogNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

我知道可以按CatalogNo分组,但是数量的总和会返回错误的数字。

有人能建议我如何修复它吗?

2个回答

3

只需修改GROUP BY子句:

group by d.DeviceId, d.CatalogNo,d.Manufacturer,d.Name

1
你需要在Group By子句中添加d.CatalogNo和d.Manufacturer。
尝试以下代码:
select d.CatalogNo,d.Manufacturer,d.Name, sum(quantity) 
from DeviceInstance di 
full join Device d 
on d.Id=di.DeviceId 
group by d.Name, d.CatalogNo,d.Manufacturer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接