2

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.

1 Answer 1

2

I think you need to do this in a subquery. Something like:

select first_action, last_action, count(1)
from (
    select
        date_trunc('month', visits.created_at) as first_action,
        max(date_trunc('month', visits.created_at)) as last_action
    from visits
    join users on users.id = visits.user_id
    where users.created_at >= ?
    group by user_id
)
group by first_action, last_action;

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.)

def date_trunc_month(field)
  Arel::Nodes::NamedFunction.new(
    'date_trunc', [Arel.sql("'month'"), field])
end

def max(*expressions)
  Arel::Nodes::Max.new(expressions)
end

users = User.arel_table
visits = Visit.arel_table

user_visits = visits.
    join(users).on(visits[:user_id].eq(users[:id])).
    where(users[:created_at].gteq(12.months)).
    group(users[:id]).
    project(
        users[:id],
        date_trunc_month(visits[:created_at]).as('first_visit'),
        max(date_trunc_month(visits[:created_at])).as('last_visit')
    ).
    as('user_visits')

cohort_data = users.
    join(user_visits).on(users[:id].eq(user_visits[:id])).
    group(user_visits[:first_visit], user_visits[:last_visit]).
    project(
        user_visits[:first_visit],
        user_visits[:last_visit],
        Arel::Nodes::Count.new([1]).as('count')
    )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.