Skip to content

Instantly share code, notes, and snippets.

@guillaumelachaud
Last active October 5, 2021 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guillaumelachaud/1142e13e014e01b12a5de3f30107c63e to your computer and use it in GitHub Desktop.
Save guillaumelachaud/1142e13e014e01b12a5de3f30107c63e to your computer and use it in GitHub Desktop.
A raw funnel SQL query
WITH
signup_occurrences AS (
SELECT
DISTINCT tracks.timestamp AS timestamp,
orgs.org_id,
tracks.event
FROM
`lazylantern.junon_webapp.tracks` tracks
LEFT JOIN
`lazylantern.junon_webapp.groups` orgs
ON
orgs.user_id = tracks.user_id
WHERE
event = 'sign_up'
GROUP BY
1,
2,
3 ),
add_credit_card_occurrences AS (
SELECT
DISTINCT tracks.timestamp AS timestamp,
orgs.org_id,
tracks.event
FROM
`lazylantern.junon_webapp.tracks` tracks
LEFT JOIN
`lazylantern.junon_webapp.groups` orgs
ON
orgs.user_id = tracks.user_id
WHERE
event = 'add_credit_card' ),
signed_form_occurrences AS (
SELECT
DISTINCT tracks.timestamp AS timestamp,
orgs.org_id,
tracks.event
FROM
`lazylantern.junon_webapp.tracks` tracks
LEFT JOIN
`lazylantern.junon_webapp.groups` orgs
ON
orgs.user_id = tracks.user_id
WHERE
event = 'signed_form' ),
orgz_never_used_support AS (
SELECT
DISTINCT org_id
FROM
`lazylantern.junon_webapp.groups` orgs EXCEPT DISTINCT (
SELECT
DISTINCT orgs.org_id,
FROM
`lazylantern.junon_webapp.tracks` tracks
LEFT JOIN
`lazylantern.junon_webapp.groups` orgs
ON
orgs.user_id = tracks.user_id
WHERE
event = 'hello') ),
funnel_step_1 AS (
SELECT
so.org_id,
so.timestamp AS step_1_date,
adc.timestamp AS step_2_date,
FROM
signup_occurrences so
LEFT JOIN
add_credit_card_occurrences adc
ON
so.org_id = adc.org_id
AND TIMESTAMP_DIFF(adc.timestamp, so.timestamp, MINUTE) <= 5
AND adc.timestamp > so.timestamp
WHERE
adc.org_id IS NOT NULL ),
funnel_step_2 AS (
SELECT
fs1.org_id AS orgId,
*
FROM
funnel_step_1 fs1
LEFT JOIN
signed_form_occurrences sfo
ON
fs1.org_id = sfo.org_id
AND TIMESTAMP_DIFF(sfo.timestamp, fs1.step_2_date, MINUTE) <= 5
AND sfo.timestamp > fs1.step_2_date
WHERE
sfo.org_id IS NOT NULL )
SELECT
DISTINCT orgId
FROM
funnel_step_2 AS funnel
WHERE
funnel.orgId IN (
SELECT
*
FROM
orgz_never_used_support)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment