Getting started: Retention Cohorts

All of your session data points are stored in fact_sessions. Using this table, we can calculate retention cohort metrics similar to those found on the dashboard.

set app_id=12345;
set cohort_start_date='2017-08-01';
set cohort_end_date='2017-08-31';
set still_active_on_start_date='2017-09-01';
set still_active_on_end_date='2017-09-30';

WITH first_session_cohort AS
  (SELECT localytics_device_id
   FROM   fact_sessions
   WHERE  app_id = $app_id
      AND first_session_date BETWEEN $cohort_start_date AND $cohort_end_date
          -- client_date filter is present here only for query optimization
      AND client_date between $cohort_start_date AND dateadd(DAY, 1, $cohort_end_date)
   GROUP BY localytics_device_id)
SELECT first_session_date                      AS first_session_cohort_date,
       client_date                             AS active_on_date,
       count(DISTINCT fs.localytics_device_id) AS distinct_users_metric
FROM fact_sessions fs
JOIN first_session_cohort fsc
ON fs.localytics_device_id = fsc.localytics_device_id
WHERE first_session_date between $cohort_start_date AND $cohort_end_date
  AND client_date between $still_active_on_start_date AND $still_active_on_end_date
  AND app_id = $app_id
GROUP BY first_session_date,
         client_date
ORDER BY first_session_date ASC,
         client_date ASC
LIMIT  30000;