SQLTeam.com | Weblogs | Forums

"not contained in either an aggregate function or the GROUP BY"

Hello,

Can you clarify please what should be included in the aggregate function and its 'group by'?

I very commonly get the error 'field is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.'

I simply do;
SELECT col1, count(col1), col2
from table1
group by col1
having count(col1)>1

Any idea?

Thanks!

you need to include col2 also in the group by.

1 Like

Thanks! It's a bit puzzling because technically I want to group only by col1 and not col2 but it worked thanks!

You can sometimes "cheat" the system by including such a column in a MAX (usually) or MIN aggregate so that you don't need to include it in the GROUP BY.