Skip to content

Instantly share code, notes, and snippets.

@linkerzx
Last active December 18, 2018 02:42
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 linkerzx/3d799faa5cf4b9df49b71f9eb91a2f49 to your computer and use it in GitHub Desktop.
Save linkerzx/3d799faa5cf4b9df49b71f9eb91a2f49 to your computer and use it in GitHub Desktop.
SELECT
dc.CustomerId,
dc.AcquisitionDate,
DATEDIFF(ac.ActivityDate, dc.AcquisitionDate) AS DaySinceAcquisition,
SUM(ac.D1Purchases) A D1Purchases
FROM dim_cust_purchaser dc
LEFT OUTER JOIN (
SELECT
CustomerId,
ActivityDate,
SUM(D1Quantity) AS D1Purchases
FROM agg_daily_activity
WHERE
ActivityType IN ('purchase')
AND ActivityDate >= '<MIN_DATE>'
GROUP BY 1,2
) ac
ON dc.CustomerId = ac.CustomerId
AND dc.AcquisitionDate <= ac.ActivityDate
WHERE
dc.AcquisitionDate >= '<MIN_DATE>'
GROUP BY 1,2,3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment