Skip to content

Instantly share code, notes, and snippets.

@limata3
Created August 18, 2021 16:47
Show Gist options
  • Select an option

  • Save limata3/243708d8d4f94d48605a3637fb3e5bb7 to your computer and use it in GitHub Desktop.

Select an option

Save limata3/243708d8d4f94d48605a3637fb3e5bb7 to your computer and use it in GitHub Desktop.
Ranking customer touchpoints to be used in attribution modelling
SELECT
customer_id
, touchpoint_id
, lead_id
, order_id
, touchpoint_channel
, touchpoint_campaign
, touchpoint_datetime
, RANK() OVER (PARTITION BY customer_id ORDER BY touchpoint_datetime ASC, rand() ) AS touchpoints_customer_rank_asc /* touchpoint -> customer */
, RANK() OVER (PARTITION BY customer_id ORDER BY touchpoint_datetime DESC, rand() ) AS touchpoints_customer_rank_des /* touchpoint -> customer */
, RANK() OVER (PARTITION BY customer_id, lead_id ORDER BY touchpoint_datetime ASC, rand() ) AS touchpoints_customer_lead_rank_asc /* touchpoint -> customer, lead */
, RANK() OVER (PARTITION BY customer_id, lead_id ORDER BY touchpoint_datetime DESC, rand() ) AS touchpoints_customer_lead_rank_desc/* touchpoint -> customer, lead */
, RANK() OVER (PARTITION BY customer_id, order_id ORDER BY touchpoint_datetime ASC, rand() ) AS touchpoints_customer_order_rank_asc /* touchpoint -> customer, order */
, RANK() OVER (PARTITION BY customer_id, order_id ORDER BY touchpoint_datetime DESC, rand() ) AS touchpoints_customer_order_rank_desc /* touchpoint -> customer, order */
FROM
master_customer_tocuhpoints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment