SQL Query Syntax

I'm trying to use the GROUP BY statement but I'm getting an error message when I try to execute my query. The error states: Column 'dbo.jomast.fsono' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Not sure I understand what I'm doing wrong. Can anyone shed some light on this?

Here's the query:

SELECT        J2.fjobno AS JobOrderNo, J2.fsono + LTRIM(RTRIM(J2.fkey)) AS SOKey, I.fcpro_name AS WorkCenterName
FROM            dbo.jodrtg AS J1 INNER JOIN
                         dbo.inwork AS I ON J1.fpro_id = I.fcpro_id INNER JOIN
                         dbo.jomast AS J2 ON J1.fjobno = J2.fjobno
WHERE        (I.fcpro_name LIKE '%-G%')
GROUP BY J2.fjobno

Open in new window

LVL 1
Lawrence SalvucciInformation Technology ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
SELECT        J2.fjobno AS JobOrderNo, J2.fsono + LTRIM(RTRIM(J2.fkey)) AS SOKey, I.fcpro_name AS WorkCenterName
FROM            dbo.jodrtg AS J1 INNER JOIN
                         dbo.inwork AS I ON J1.fpro_id = I.fcpro_id INNER JOIN
                         dbo.jomast AS J2 ON J1.fjobno = J2.fjobno
WHERE        (I.fcpro_name LIKE '%-G%')
GROUP BY J2.fjobno, J2.fsono + LTRIM(RTRIM(J2.fkey)),  I.fcpro_name

Open in new window


You need to include J2.fsono + LTRIM(RTRIM(J2.fkey)) and I.fcpro_name in GROUP BY or

Apply an aggregate function to them like:
SELECT        J2.fjobno AS JobOrderNo, FIRST( J2.fsono + LTRIM(RTRIM(J2.fkey))) AS SOKey, MAX(I.fcpro_name)) AS WorkCenterName
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.