I have the following postgresql statement which works ok in navicat for Postgresql,
SELECT date_trunc('day', updated_at), count(*) as total_count
FROM "persons"
WHERE ("persons"."updated_at" BETWEEN '2012-10-17 00:00:00.000000' AND '2012-11-07 12:25:04.082224')
GROUP BY date_trunc('day',updated_at)
ORDER BY count(updated_at) DESC
I get expected results like
2012-10-31 00:00:00,5
2012-11-06 00:00:00,2
2012-11-05 00:00:00,1
I need to convert this to ruby for a RoR project
I've written this statement as follows
persons = where(updated_at: start.beginning_of_day..Time.zone.now)
persons = persons.group("date(updated_at)")
persons = persons.select("updated_at, count(*) as total_count")
persons = persons.order("count(updated_at)")
however i get the following error
PG::Error: ERROR: column "persons.updated_at" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT updated_at, count(*) as total_count FROM "persons" WH...
^
: SELECT updated_at, count(*) as total_count FROM "persons" WHERE ("persons"."updated_at" BETWEEN '2012-10-17 00:00:00.000000' AND '2012-11-07 14:38:54.674684') GROUP BY date(persons.updated_at) ORDER BY count(updated_at)
any ideas on what i'm doing wrong?