Each GROUP BY expression must contain at least one column that is not an outer reference.

  • Hi all

    I'm new with SQL SERVER. I came across the following error wich I'm having a hard time to solve it.

    Error: "Msg 164, Level 15, State 1, Procedure test, Line 11 Each GROUP BY expression must contain at least one column that is not an outer reference."

    here is my code.

    SELECT 'MonthYear', SUM('Total') AS Total

    FROM (SELECT CAST(DATENAME(M, Date) AS varchar) + '-' + CAST(YEAR(Date) AS varchar) AS 'MonthYear', SUM(Amount) AS 'Total'

    FROM dbo.hardware

    GROUP BY CAST(DATENAME(M, Date) AS varchar) + '-' + CAST(YEAR(Date) AS varchar)

    UNION ALL

    SELECT CAST(DATENAME(M, Date) AS varchar) + '-' + CAST(YEAR(Date) AS varchar) AS 'MonthYear', SUM(Amount) AS 'Total'

    FROM dbo.accesories

    GROUP BY CAST(DATENAME(M, Date) AS varchar) + '-' + CAST(YEAR(Date) AS varchar)) AS tbtemp

    GROUP BY 'monthyear'

    Thank you

  • Hi,

    Post your code

  • SELECT [MonthYear], SUM([Total]) AS Total

    FROM

    (

    SELECT CAST(DATENAME(M, Date) AS varchar) + '-' + CAST(YEAR(Date) AS varchar) AS 'MonthYear',

    SUM(Amount) AS 'Total'

    FROM dbo.hardware

    GROUP BY CAST(DATENAME(M, Date) AS varchar) + '-' + CAST(YEAR(Date) AS varchar)

    UNION ALL

    SELECT CAST(DATENAME(M, Date) AS varchar) + '-' + CAST(YEAR(Date) AS varchar) AS 'MonthYear',

    SUM(Amount) AS 'Total'

    FROM dbo.accesories

    GROUP BY CAST(DATENAME(M, Date) AS varchar) + '-' + CAST(YEAR(Date) AS varchar)

    ) AS tbtemp

    GROUP BY [monthyear]

  • Get rid of your apostrophes. It thinks you're talking about the string 'MonthYear', not the column within your query.

    SELECT MonthYear....

    FROM

    (

    SELECT ... AS MonthYear, ...

    )...

    GROUP BY MonthYear

    Rob Farley
    LobsterPot Solutions & Adelaide SQL Server User Group
    Company: http://www.lobsterpot.com.au
    Blog: http://blogs.lobsterpot.com.au

  • Thanks for your replies

    But both give me an Invalid Column name error.

    Sorry that part of my code was missing.

    Create View "Orders" as

  • Viper5646 (12/23/2009)


    Thanks for your replies

    But both give me an Invalid Column name error.

    Sorry that part of my code was missing.

    Create View "Orders" as

    Hi,

    you mean,

    create view [ORDER]

    as

    SELECT [MonthYear], SUM([Total]) AS Total

    FROM

    (

    SELECT CAST(DATENAME(M, Date) AS varchar) + '-' + CAST(YEAR(Date) AS varchar) AS 'MonthYear',

    SUM(Amount) AS 'Total'

    FROM dbo.hardware

    GROUP BY CAST(DATENAME(M, Date) AS varchar) + '-' + CAST(YEAR(Date) AS varchar)

    UNION ALL

    SELECT CAST(DATENAME(M, Date) AS varchar) + '-' + CAST(YEAR(Date) AS varchar) AS 'MonthYear',

    SUM(Amount) AS 'Total'

    FROM dbo.accesories

    GROUP BY CAST(DATENAME(M, Date) AS varchar) + '-' + CAST(YEAR(Date) AS varchar)

    ) AS tbtemp

    GROUP BY [monthyear]

  • I assume you have columns called Date and Amount in each of your tables dbo.hardware and dbo.accessories...

    Rob Farley
    LobsterPot Solutions & Adelaide SQL Server User Group
    Company: http://www.lobsterpot.com.au
    Blog: http://blogs.lobsterpot.com.au

  • Thank you SSC Eight & SSC-Enthusiastic for your help.

    I have posted SSC Eight's code and it worked πŸ™‚

  • Hi Viper,

    Both tell you the same concept which you not made in your code. However you got the concept that’s enough.

  • It's "Arun" and "Rob". You're reading the forum status (like yours is "Forum Newbie")

    Rob Farley
    LobsterPot Solutions & Adelaide SQL Server User Group
    Company: http://www.lobsterpot.com.au
    Blog: http://blogs.lobsterpot.com.au

  • πŸ˜€ Sorry about that

    Merry Christmas And a Happy New Year

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply