Skip to content

Instantly share code, notes, and snippets.

@SegFaultAX
Created February 7, 2013 23:45
Show Gist options
  • Save SegFaultAX/4735303 to your computer and use it in GitHub Desktop.
Save SegFaultAX/4735303 to your computer and use it in GitHub Desktop.
Simple Cohort [MySQL]
SELECT
cohorts.first_action,
cohorts.last_action,
COUNT(cohorts.id) AS user_count
FROM (SELECT
MIN(DATE(created_at - INTERVAL (DAYOFWEEK(created_at) - 1) DAY)) AS first_action,
MAX(DATE(created_at - INTERVAL (DAYOFWEEK(created_at) - 1) DAY)) AS last_action,
user_id AS id
FROM daily_activity_events
GROUP BY user_id) AS cohorts
GROUP BY
cohorts.first_action,
cohorts.last_action
ORDER BY
cohorts.first_action,
cohorts.last_action;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment