it fails with "PG::GroupingError: ERROR: column "utility_data.id" must
appear in the GROUP BY clause or be used in an aggregate function" I
THINK what's happening is that the working version generates the
following SQL:
SELECT utility_id, sum(ami_residential)
FROM "utility_data"
WHERE "utility_data"."utility_id" = $1
GROUP BY utility_id [["utility_id", 5621]]
whereas the failing version adds "ORDER BY" and "LIMIT" to the query:
SELECT utility_id, sum(ami_residential)
FROM "utility_data"
WHERE "utility_data"."utility_id" = $1
GROUP BY utility_id
ORDER BY "utility_data"."id" ASC
LIMIT 1 [["utility_id", 5621]]
which triggers the grumpy ol' postgresql error.
So two questions:
- is this expected behavior?
- is there a way to inhibit the addition of ORDER BY and LIMIT?
If you just want data rolled up for *one* Utility (I'm
guessing that's what `u` in the code above is...)
you could skip the group:
sum = u.utility_data.sum(:ami_residential)
You are spot on: in this case I'm just considering one Utility at a
time. I don't know what possessed me to include the GROUP clause since
the WHERE clause alone is sufficient. Time to go back to SQL camp...