gpt4 book ai didi

sql - Group By 子句导致错误

转载 作者:行者123 更新时间:2023-12-04 14:36:28 24 4
gpt4 key购买 nike

所以这里是上下文:开发 ASP.NET MVC 4 Web 应用程序,我的数据库中有一个表 产品分配它由 2 个外键组成:一个来自我的表 产品展示 另一个来自表 .我还有一张 table ,车辆 其中包含表 的外键产品展示

我想选择按产品分组的分配及其信息(一个产品可以多次分配)。这是我的存储过程:

ALTER PROCEDURE GetAllVehicles

AS

BEGIN

SET NOCOUNT ON

SELECT

p.FirstName,
p.LastName,
pa.EndDate,
pr.PurchaseDate,
pr.SerialNumber,
pr.CatalogPrice,
v.PlateNumber,
v.FirstCirculationDate,
V.FirstDrivingTax,
v.UsualDrivingTax

FROM bm_ProductAllocations AS pa

INNER JOIN bm_Persons AS p ON pa.Id_Person = p.Id_Person
INNER JOIN bm_Products AS pr ON pa.Id_Product = pr.Id_Product
INNER JOIN bm_Vehicles AS v ON pr.Id_Product = v.Id_Product

GROUP BY pa.Id_Product

END

但是,Group By 子句生成错误: Column 'bm_Persons.FirstName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.我正在使用 Visual Studio 2010。

我是 SQL 的新手,所以我不知道发生了什么。

最佳答案

您分组的字段,您需要使用聚合 sum , max等或者您需要在子句中包含列,请参阅以下链接:SQL Group By & summarizing values

SELECT   p.FirstName
,p.LastName
,pa.EndDate
,pr.PurchaseDate
,pr.SerialNumber
,pr.CatalogPrice
,v.PlateNumber
,v.FirstCirculationDate
,v.FirstDrivingTax
,v.UsualDrivingTax
FROM bm_ProductAllocations AS pa
INNER JOIN bm_Persons AS p ON pa.Id_Person = p.Id_Person
INNER JOIN bm_Products AS pr ON pa.Id_Product = pr.Id_Product
INNER JOIN bm_Vehicles AS v ON pr.Id_Product = v.Id_Product
GROUP BY pa.Id_Product
,p.FirstName
,p.LastName
,pa.EndDate
,pr.PurchaseDate
,pr.SerialNumber
,pr.CatalogPrice
,v.PlateNumber
,v.FirstCirculationDate
,v.FirstDrivingTax
,v.UsualDrivingTax;

关于sql - Group By 子句导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16314836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com