Skip to content

Instantly share code, notes, and snippets.

@x3mxray
Last active April 11, 2024 21:51
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 x3mxray/df25c85e898bbfa953e1e0c09995f1da to your computer and use it in GitHub Desktop.
Save x3mxray/df25c85e898bbfa953e1e0c09995f1da to your computer and use it in GitHub Desktop.
Sitecore CDP Batch Segmentation - Risky of churn customers
SELECT g.meta_ref as guest_ref
FROM (
SELECT o.meta_guest_ref,
NTILE(5) OVER (ORDER BY R DESC) as R_S,
NTILE(5) OVER (ORDER BY F ASC) as F_S,
NTILE(5) OVER (ORDER BY M ASC) as M_S
FROM (
SELECT o.meta_guest_ref,
DATE_DIFF('day', max(date(core_ordered_at)), current_date) AS R,
COUNT(*) AS F,
SUM(o.core_price) AS M
FROM orders o
WHERE date(o.core_ordered_at) > date('2023-01-01')
GROUP BY o.meta_guest_ref
) o
)
INNER JOIN guests g
ON meta_guest_ref = g.meta_ref
WHERE R_S<3 and F_S=5 and M_S>3
GROUP BY g.meta_ref
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment