【问题标题】:Dupe: Each GROUP BY expression must contain at least one column that is not an outer reference欺骗:每个 GROUP BY 表达式必须至少包含一个不是外部引用的列
【发布时间】:2011-10-19 02:34:51
【问题描述】:

我在尝试创建以下存储过程时遇到以下错误。

存储过程:

CREATE PROCEDURE spReport

    @rLevel nvarchar(50),
    @aSelected nvarchar(64),
    @wType nvarchar(20),
    @dSelected nvarchar(20),
    @StartDate smalldatetime,
    @EndDate smalldatetime

AS
BEGIN


    SET NOCOUNT ON;

    declare @total float 

    if (@dSelected = null)
    begin
        select @total = sum(total) from bs where @rLevel =  CHAR(39)+ @aSelected+CHAR(39) 
                 and wtype = '' + @wType + '' and bdate between CHAR(39)+@StartDate+CHAR(39) and CHAR(39)+@EndDate+CHAR(39) 

        select @rLevel, bdays, str(sum(total*100/@total ),38,2) as percentages, sum(total)as total1 
        from bs 
        where @rLevel= CHAR(39)+@aSelected+CHAR(39) and wtype = CHAR(39)+@wType+CHAR(39) and 
        bdate between CHAR(39)+@StartDate+CHAR(39) and  CHAR(39)+@EndDate+CHAR(39)
        group by @rLevel, bdays 
        order by  
        CASE @rLevel
            WHEN 'r' THEN r
            WHEN 'D' THEN D 
            WHEN 'S' THEN S             
        END
        ,bdays

    end
END

错误: 每个 GROUP BY 表达式必须至少包含一个不是外部引用的列。

我不明白我做错了什么。

除此之外我在使用单引号时遇到问题,我使用它的方式是否正确?

【问题讨论】:

标签: sql tsql sql-server-2008 stored-procedures


【解决方案1】:

您不需要按常量表达式进行分组,@rLevel 就是其中之一。

所以,当你删除它时......你会遇到另一个问题。这是你的ORDER BY 子句。它包含对既不聚合也不包含在GROUP BY 中的列的引用。 (当您的语句中有 GROUP BY 子句时,您不能按任意列排序。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    相关资源
    最近更新 更多