Skip to content
Advertisement

Cannot use an aggregate or a subquery

I’m trying to group this case expression but unfortunately I’m getting an error.

ERROR:

Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.

Is there anyway I can make this work?

Advertisement

Answer

I have a couple of suggestions. You are using select * in your case statements , but the columns referenced by ‘*’ are not in your group by clause. To add them would be a bad way to fix the problem. Instead use ‘ select 1 ‘ and see what happens.

Second recommendation is to just not use the when exists rather use left join using sub queries and use those in your case statements.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement