Skip to content

Instantly share code, notes, and snippets.

View jthandy's full-sized avatar

Tristan Handy jthandy

View GitHub Profile
@jthandy
jthandy / data_import_api_call.rb
Last active December 16, 2015 03:49
A simple code snippet to post data to the RJMetrics Data Import API.
def push_to_warehouse(table, data_to_push)
response = HTTParty.post("https://connect.rjmetrics.com/v1/client/#{cid}/table/#{table}/data?apikey=#{apikey}",
body: data_to_push.to_json,
headers: { 'Content-Type' => 'application/json' })
response
end
@jthandy
jthandy / data_scientists.sql
Created October 7, 2015 15:56
The query we used in the RJMetrics benchmark report "The State of Data Science" to identify data scientist profiles.
/*
Look for Data Scientists in the following 9 languages:
-- English
-- Spanish
-- French
-- German
-- Dutch
-- Swedish
-- Italian
Verifying that +jthandy is my blockchain ID. https://onename.com/jthandy
select
customer,
timestamp 'epoch' + date * interval '1 Second' as date,
forgiven,
subscription_id,
paid,
total,
timestamp 'epoch' + period_start * interval '1 Second' as period_start,
timestamp 'epoch' + period_end * interval '1 Second' as period_end
with invoices as (
select *
from {{env.schema}}.stripe_invoices_cleaned
where paid is true
and forgiven is false
),
days as (
select (min(period_start) over () + row_number() over ())::date as date_day
from invoices
), months as (
select distinct date_trunc('month', date_day)::date as date_month
from days
where date_day <= current_date
customers as (
select customer, min(period_start) as active_from, max(period_end) as active_to
from invoices
where period_start <= current_date
group by customer
), customer_dates as (
select m.date_month, c.customer
select date_month, d.customer, period_start, period_end,
"interval" as period,
case "interval"
when 'yearly'
then coalesce(i.total, 0)::float / 12 / 100
else
coalesce(i.total, 0)::float / 100
end as total,
case min(date_month) over(partition by d.customer)
with invoices as (
select *
from {{env.schema}}.stripe_invoices_transformed
), all_months as (
select distinct date_month from invoices
),
plan_changes as (
select
*,
lag(total) over (partition by customer order by date_month) as prior_month_total,
total - coalesce(lag(total) over (partition by customer order by date_month), 0) as change,
lag(period_end) over (partition by customer order by date_month) as prior_month_period_end
from invoices
),