Skip to content

Instantly share code, notes, and snippets.

@omarqureshi
Created January 27, 2014 14:44
Show Gist options
  • Save omarqureshi/8649747 to your computer and use it in GitHub Desktop.
Save omarqureshi/8649747 to your computer and use it in GitHub Desktop.
with stddeviation as (
select stddev(extract (epoch from coalesce(deactivated_at, now()) - created_at)) as s from users where type = 'ServiceUser'
),
average as (
select avg(extract (epoch from coalesce(deactivated_at, now()) - created_at)) as a from users where type = 'ServiceUser'
)
select avg(coalesce(deactivated_at, now()) - created_at) as average
from users, average, stddeviation
where type = 'ServiceUser'
and extract(epoch from coalesce(deactivated_at, now()) - created_at) < average.a + (stddeviation.s * 2)
and extract(epoch from coalesce(deactivated_at, now()) - created_at) > average.a - (stddeviation.s * 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment