即使字段包含在分组依据列表中,分组依据或聚合错误
Posted
技术标签:
【中文标题】即使字段包含在分组依据列表中,分组依据或聚合错误【英文标题】:Group by or aggregate error even though field is included in group by list 【发布时间】:2016-05-01 00:06:39 【问题描述】:我有一个 SQL 多值分组查询,我想在其中找到字段 slt.OUTCOST
的最大值。运行它会导致以下错误:
列 '#eldekiSeri.CODE' 在选择列表中无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。"
即使GROUP BY
子句中包含该字段,我也会收到此错误。
如果我删除 MAX 聚合并将 slt.OUTCOST
包含在组列表中,错误就会消失,但我再也找不到该字段的最大值了。
这是我的 SQL:
set @sqlAbuk = '
SELECT
eldekiSeri.CODE AS [SERİ NO],
eldekiSeri.KODU AS [STOK KODU],
eldekiSeri.ACIKLAMA AS [STOK ADI],
clc.CODE AS [CARİ KOD],
clc.DEFINITION_ AS [CARİ ADI],
MAX(slt.OUTCOST) AS [ALIŞ FİYATI],
stf.DATE_ AS [ALIŞ TARİHİ],
eldekiSeri.AMBARNO AS [ŞUBE KODU]
FROM '+@firmDb+'.dbo.LG_'+@firmNumber+'_'+@firmPeriod+'_SERILOTN sl
INNER JOIN '+@firmDb+'.dbo.LG_'+@firmNumber+'_'+@firmPeriod+'_SLTRANS slt ON sl.LOGICALREF = slt.SLREF
INNER JOIN #eldekiSeri eldekiSeri ON sl.CODE = eldekiSeri.CODE
INNER JOIN '+@firmDb+'.dbo.LG_'+@firmNumber+'_'+@firmPeriod+'_STFICHE stf ON slt.STFICHEREF = stf.LOGICALREF
INNER JOIN '+@firmDb+'.dbo.LG_'+@firmNumber+'_CLCARD clc ON stf.CLIENTREF = clc.LOGICALREF
WHERE (slt.INVENNO = 0) AND (slt.FICHETYPE = 1)';
--print @sqlAbuk;
--exec (@sqlAbuk);
set @sqlAbuk = @sqlAbuk +' AND eldekiSeri.CODE not in ( select distinct bc.[SERİ NO] from #birlestirilmisClone bc ) ';
set @sqlAbuk = @sqlAbuk +' GROUP BY eldekiSeri.CODE, eldekiSeri.KODU, eldekiSeri.ACIKLAMA, clc.CODE, clc.DEFINITION_, stf.DATE_, eldekiSeri.AMBARNO';
set @sqlAbuk = @sqlAbuk +' ORDER BY eldekiSeri.CODE, eldekiSeri.KODU, eldekiSeri.ACIKLAMA, clc.CODE, clc.DEFINITION_, slt.OUTCOST, stf.DATE_, eldekiSeri.AMBARNO';
print @sqlAbuk;
--Exec(@sqlAbuk);
insert into #birlestirilmis Exec(@sqlAbuk);
--print (@sqlAbuk);
【问题讨论】:
【参考方案1】:这是因为您的 ORDER BY 子句中有 slt.OUTCOST。删除它,它将消除您的错误。
【讨论】:
以上是关于即使字段包含在分组依据列表中,分组依据或聚合错误的主要内容,如果未能解决你的问题,请参考以下文章