create proc proc_GetLikeSupplier
@GoodsName varchar(30),
@Type bit
as
declare @GoodID int
set @GoodID = -1
-- 开始发现商品
if(@Type = 0)
select @GoodID = GoodID from Goods where GoodName = @GoodsName
else
select @GoodID = ProductID from Product where ProductName = @GoodsName

-- ~ end
if(@GoodID = -1)   -- 没有发现商品
return
 
select 
sps.ComName,
sps.Name,
sps.Addres,
sps.MobileNum,
spsSP.Prices,
A1 = (select COUNT(Prices) from PurchaseLog pl where pl.SupplierID = sps.SupplierID),
A2 = (select SUM(Prices * [Sum]) from PurchaseLog p1 where p1.SupplierID = sps.SupplierID),
A3 = (select count(prices) from PurchaseLog pl where pl.supplierID = sps.SupplierID and Stateds = 2)
from Suppliers sps,SupplierSP spsSP 
where 
sps.SupplierID = spsSP.SupplierID 
and
spsSP.GoodID = @GoodID
and 
spsSP.[Type] = @Type
and 
sps.SupplierID in (select SupplierID from Suppliers group by SupplierID)
group by sps.SupplierID,spsSP.SupplierID,sps.ComName,sps.Addres,sps.Name,sps.MobileNum,spsSP.Prices
go
消息 8124,级别 16,状态 1,过程 proc_GetLikeSupplier,第 24 行
在包含外部引用的被聚合表达式中指定了多个列。如果被聚合的表达式包含外部引用,那么该外部引用就必须是该表达式中所引用的唯一的一列。
这是啥原因

解决方案 »

  1.   

    A3 = (select count(prices) from PurchaseLog pl 
    where pl.supplierID = sps.SupplierID and (这里是否少一个别名呢)Stateds = 2)
      

  2.   

    sps.SupplierID in (select SupplierID from Suppliers group by SupplierID)
    --这句多余
    group by sps.SupplierID,spsSP.SupplierID,sps.ComName,sps.Addres,sps.Name,sps.MobileNum,spsSP.Prices
    --从你的分组 和 查询结果 看,是得不到你想要的结果的
    --关键在分组的 sps.SupplierID,spsSP.SupplierID,spsSP.Prices 这里 
      

  3.   

    回复楼上 
    sps.SupplierID in (select SupplierID from Suppliers group by SupplierID)
    是因为 group by 需要将所有列都加入到group语句中 所以
    我干脆就先使用in过滤掉重复的SupplierID(因为它是唯一的标识)-- 
    -- 查出所有供应商它们的ComName,Name,MobileNum,Address,Prices,
    -- 以及向该公司的采购次数,累积采购总额,累积退货次数这是我想要的效果 听起来很悲催对吧???
      

  4.   

    分别用两个临时表得到供应商详细信息#t1、供应商各汇总信息#t2,然后根据供应商编号左联接查询:
    select * from #t1 left join #t2 on #t1.供应商编号=#t2.供应商编号