Skip to content Skip to sidebar Skip to footer

Aggregates Not Allowed In Where Clause In Postgresql Error

I was trying to execute this following query, and I am a beginner in writing SQL Queries, was wondering how can I achieve the following aggregate functions functionality doing in W

Solution 1:

You can do this with a window function in a subquery:

select name, add, mobile
from (select a.name, a.add, a.mobile, total,
             avg(ac.total) over (partition by a.name, a.add, a.mobile) as avgtotal, a.total
      from user a INNER JOIN
           user_info aac
           ON aac.userid= a.userid INNER JOIN
           info ac 
           ON aac.infoid= ac.infoid
     ) t
WHERE total < 8 * avgtotal
GROUP BY name, add, mobile;

Post a Comment for "Aggregates Not Allowed In Where Clause In Postgresql Error"