I’m trying to do a cohort analysis query in Rails but running into trouble with the correct way to group by the last action date.
I want to end up with rows of the following data for something like this: http://www.quickcohort.com/
count first_action last_action
for all users who registered in the last year. first_action and last_action are truncated to the nearest month.
Getting the counts grouped by the first_action is easy enough, but when I try to extend it to include the last_action I encounter
ActiveRecord::StatementInvalid: PGError: ERROR: aggregates not allowed in GROUP BY clause
Here’s what I have so far
User
.select("COUNT(*) AS count,
date_trunc('month', users.created_at) AS first_action,
MAX(date_trunc('month', visits.created_at)) AS last_action # <= Problem
")
.joins(:visits)
.group("first_action, last_action") # TODO: Subquery ?
.order("first_action ASC, last_action ASC")
.where("users.created_at >= date_trunc('month', CAST(? AS timestamp))", 12.months.ago)
The visits table tracks all visits users make to the site. Using the latest visit as the last action seems like it should be easy, but I’m having trouble forming it into SQL.
I’m also open to other solutions if there are better ways, but it seems like a single SQL query would be most performant.
I think you need to do this in a subquery. Something like:
I’m not sure what the most elegant way would be to do this in ARel, but I think it’d be something like this. (Might just be easier to use the SQL directly.)