0
votes

Here is an snippet from heroku logs:

ActiveRecord::StatementInvalid (PG::GroupingError: ERROR: column "stats.Season2dateMean" must appear in the GROUP BY clause or be used in an aggregate function

app[web.1]: LINE 1: SELECT "stats"."Season2dateMean", "stats"."Season2dateOFFMea...

app[web.1]: : SELECT "stats"."Season2dateMean", "stats"."Season2dateOFFMean", "stats"."Season2dateDEFMean" FROM "stats" GROUP BY "stats"."team1" HAVING created_at = MAX(created_at)):

Below is the excerpt from my controller file where the error occurs:

Return all Stats and group results by team1 attribute

@a = Stat.all
@b = @a.group("stats.team1")

Find last created record for each team

@c = @b.having("created_at = MAX(created_at)")

Return an array of the three selected attributes

@teamValues = @c.pluck("Season2dateMean", "Season2dateOFFMean", "Season2dateDEFMean")

How would I correct this error or is it not possible?

1
Can you include the error? You can extract it by typing heroku logs in the terminal - Hristo Georgiev
@HristoGeorgiev i added the error log above. Thanks - Anees
I installed PostgreSQL locally so I can catch these errors in development.. Here is the error in development enviroment: PG::GroupingError: ERROR: column "stats.Season2dateMean" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: SELECT "stats"."Season2dateMean", "stats"."Season2dateOFFMea... ^ : SELECT "stats"."Season2dateMean", "stats"."Season2dateOFFMean", "stats"."Season2dateDEFMean" FROM "stats" GROUP BY "stats"."team1" HAVING created_at = MAX(created_at) - Anees

1 Answers

0
votes

Figured it out! Here are my changes:

@a = Stat.all
@b = @a.group("Season2dateMean", "Season2dateOFFMean", "Season2dateDEFMean", "team1", "created_at")
@c = @b.having("created_at = MAX(created_at)")
@teamValues = @c.pluck("Season2dateMean", "Season2dateOFFMean", "Season2dateDEFMean")

The difference is including all attributes in the group by in my second object. Best idea is to just install postgreSQL and use it locally.