I am trying to write a SQL that uses information from two tables.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lynn Hall

    I am trying to write a SQL that uses information from two tables.

    Code:
    [SELECT Last_name AS 'Last Name',[EEO-1 Classification]
      FROM Employee
    	INNER JOIN Job
      ON Employee.JobID=Job.JobID
      GROUP BY [EEO-1 Classification]
    ]
    When I run the program I receive an error:
    Msg 8120, Level 16, State 1, Line 3
    Column 'Employee.Last_ name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
    What am I doing wrong?
    Last edited by NeoPa; Oct 11 '10, 03:07 PM. Reason: Please use the [code] tags provided.
  • gpl
    New Member
    • Jul 2007
    • 152

    #2
    Group By allows you to show some detail (max, count, sum etc) for the group ... you have used the group [EEO-1 Classification] - what do you want to do with the 'Employee.Last_ name' ? The number of employees in each group, the first or last name alphabetically ?

    What exactly are you trying to achieve with this query ?

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32634

      #3
      Originally posted by Lynn Hall
      Lynn Hall:
      What am I doing wrong?
      Apart from the brackets ([]) surrounding your SQL, you use a string ('Last Name') in your first line as an ALIAS, instead of a name ([Last Name]).

      If you change line #1 to :
      Code:
      SELECT Last_name AS [Last Name],[EEO-1 Classification]
      You should see the problem disappear.

      PS. Don't forget to remove the closing bracket (]) will you.

      PPS. GPL's post is probably a more direct response as it covers the problem indicated by the error message. In aggregated queries you can only select items that are GROUPed BY, or aggregated items (EG. Sum([...]), Max([...]), Avg([...]), Count([...]), etc).
      Last edited by NeoPa; Oct 11 '10, 03:14 PM.

      Comment

      Working...