Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2005
    Posts
    8

    Question Unanswered: Invalid Column Name...can't figure this out

    The following is an approximation of the code I'm trying:

    SELECT 'dummy' as DummyField, FieldA, FieldB, FieldC,
    FieldD, FieldE, FieldF, FieldG, FieldH, FieldI,
    FieldJ, FieldK, FieldL, AVG(AMT) AS AMT,
    AVG(QTY) AS QTY, FieldM

    FROM GetMonthlyData('200501') as i

    GROUP BY DummyField, FieldA, FieldB, FieldC,
    FieldD, FieldE, FieldF, FieldG, FieldH, FieldI,
    FieldJ, FieldK, FieldL, FieldM


    It checks out OK syntactically but whenever I try to run it I get a message like the following:

    Server: Msg 207, Level 16, State 3, Line 1
    Invalid column name 'DummyField'.
    Server: Msg 207, Level 16, State 1, Line 1
    Invalid column name 'DummyField'.


    I've also tried not giving DummyField an alias and using the 'dummy' value in the GROUP BY clause but that won't even get by the CheckMark button:

    Server: Msg 164, Level 15, State 1, Line 8
    GROUP BY expressions must refer to column names that appear in the select list.


    Advice?
    Last edited by RedDragonT; 02-25-05 at 14:21.

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    The following is an approximation of an answer

    Check the code inside your table-valued function. I'd bet that is where the problem lies.

    -PatP

  3. #3
    Join Date
    Feb 2005
    Posts
    8

    ..

    Sorry, the error message was citing 'DummyField', the alias of the expression I'm using as the problem. I've edited my original post to reflect this.

    If I use the table-valued function independently it works fine. If I remove the DummyField expression it also works fine. But I need that expression when I populate this data into a different table.

  4. #4
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    I think I have done this before. Remove dummy field from the group by but keep it in the select list. The traditional rules about have the same values in the select and the group by do not exist when you have a constant in the select.

    It works. Check it:


    CREATE TABLE Test
    (
    ID int IDENTITY(1,1),
    TheName varchar(10),
    TheNumber int
    )

    INSERT INTO Test (TheName,TheNumber)
    VALUES('Joe',2)

    INSERT INTO Test (TheName,TheNumber)
    VALUES('Joe',3)

    INSERT INTO Test (TheName,TheNumber)
    VALUES('Joe',4)

    SELECT 'MySillyExample',TheName,Sum(TheNumber) as numbers
    FROM Test
    GROUP BY TheName
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  5. #5
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    You are correct, sir!

    The values in the GROUP BY clause have to have their origins in the underlying dataset.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  6. #6
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    I get one right from time to time.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  7. #7
    Join Date
    Feb 2005
    Posts
    8

    ..

    Well that worked. Thanks Thrasymachus.

  8. #8
    Join Date
    Aug 2012
    Posts
    1

    Red face Alias help

    Right on!!!!!! thanks for help on subject

  9. #9
    Join Date
    Oct 2009
    Location
    221B Baker St.
    Posts
    486
    There is no reason to reply to a topic that has been dormant for 7 years. . .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •