小编典典

Group By子句导致错误

sql

因此,这里是上下文:开发一个ASP.NET MVC 4 Web应用程序,我在数据库中有一个表 ProductAllocations*
,该表由2个外键组成:一个来自我的表 Products ,另一个来自表 Persons 。我有另一个表 Vehicles
,其中包含表 Products 的外键
*

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

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的新手,所以我不知道发生了什么。


阅读 202

收藏
2021-04-14

共1个答案

小编典典

领域GROUP BY
,你需要或者使用聚合summax等等,或者你需要包括子句中的列,请访问以下链接:SQL集团通过
汇总值

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;
2021-04-14