1

I get this error

each group by expression must contain at least one column that is not an outer reference

I am inserting date into a temp table, and error is at the following group by query(only on some collation)

insert into @Temp(Name, ID)
   select 
      USR.Name,
      ISNULL((SELECT SUM(BCR.QUANTITY) 
              FROM Rate BCR            
              WHERE BCR.RateID = USR.RateID 
              GROUP BY USR.RateID), 0) AS TotalQuantity
   from 
      dbo.User as USR
   where 
      USR.Status = 1
4

1 回答 1

1

您有一个相关的子查询,其中 USR.RateID 来自外部查询。由于您在 where 子句中使用 USR.RateID,因此您可以删除 GROUP BY 子句,因为每次调用子查询时,USR.RateID 只会有一个值。

insert into @Temp(Name, ID)
   select 
      USR.Name,
      ISNULL((SELECT SUM(BCR.QUANTITY) 
              FROM Rate BCR            
              WHERE BCR.RateID = USR.RateID), 0) AS TotalQuantity
   from 
      dbo.User as USR
   where 
      USR.Status = 1
于 2012-11-08T06:49:17.010 回答