SQL Query examples

Example 1

The result of this query is a list of user events for the last 2 days sorted by the amount of times the event was performed.

with last_ev as 
    (select distinct devtodevid, max(eventtime) as eventtime
    from p104704.custom_events 
    where created >= current_date - interval '2 days'
    group by 1)
select name, count(distinct devtodevid)
from 
  (select distinct q1.devtodevid, name
  from p104704.custom_events q1 
    join last_ev q2 using (devtodevid, eventtime)) q3
group by 1
order by 2 desc

Example 2

The result of this query is a list of last user events before they churn. The churn period is specified.

with last_ev as
    (select distinct devtodevid, max(eventtime) as eventtime
    from p104704.custom_events ev 
    join p104704.users us using(devtodevid)
    where us.lasttime <= current_date - interval '7 days'  -- churn period
    and ev.created >= current_date - interval '30 days'
    group by 1)
select name, count(distinct devtodevid)
from
  (select distinct q1.devtodevid, name
  from p104704.custom_events q1
    join last_ev q2 using (devtodevid, eventtime)) q3
group by 1
order by 2 desc

Example 3

The result of this query is a timestamp of the last event with numeric value.

The result of this query is a timestamp of the last event with text (string) value.

Example 4

Use this query to find the users with decreasing player level (events with decreasing player level alert). The result of this query is a list of users and their previous and current levels.

Example 5

Use this query to calculate user retention. The result of this query is a table that contains the number of new users per day and the number of returning users on the 0th, 1st, 2nd, 3rd, 7th, and 14th day after installation.

Example 6

See revenue for a year by monthly cohorts. The result table will show revenue by each cohort for each month.

You can modify the periods for the cohorts and payment events.

Last updated

Was this helpful?