Sample queries
This partner page offers some sample queries of possible use cases to reference when setting up your queries.
A common query might be to filter events by time.
You can filter them by the time of occurrence. Event tables are clustered by time which makes filtering by time optimal:
1
2
3
4
5
-- find custom events that occurred after 04/15/2019 @ 7:02pm (UTC) i.e., timestamp=1555354920
SELECT *
FROM users_behaviors_customevent_shared
WHERE time > 1555354920
LIMIT 10;
You can also filter events by the time at which they were persisted in the Snowflake data warehouse by using sf_created_at. sf_created_at and time are not the same but are usually close, so this query should have similar performance characteristics:
1
2
3
4
5
-- find custom events that arrived in Snowflake after time 04/15/2019 @ 7:02pm (UTC)
SELECT *
FROM users_behaviors_customevent_shared
WHERE sf_created_at > to_timestamp_ntz('2019-04-15 19:02:00')
LIMIT 10;
The value of sf_created_at is reliable only for events that were persisted after Nov 15th, 2019 9:31 pm UTC.
You can use this push funnel query to aggregate push sends raw event data, through to deliveries raw event data, through to opens raw event data. This query shows how all the tables should be joined since each raw event typically has a separate table:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
SELECT
COUNT(DISTINCT send."ID" ) AS "users_messages_pushnotification_send.push_sent",
COALESCE((COUNT(DISTINCT send."ID" )),0)-COALESCE((COUNT(DISTINCT bounce."ID" )),0) AS "users_messages_pushnotification_send.push_delivered",
COUNT(DISTINCT open."ID" ) AS "users_messages_pushnotification_open.push_opens"
FROM users_messages_pushnotification_send_shared AS send
LEFT JOIN USERS_MESSAGES_PUSHNOTIFICATION_OPEN_shared AS open ON (send."USER_ID")=(open."USER_ID")
AND
(send."DEVICE_ID")=(open."DEVICE_ID")
AND
((send."MESSAGE_VARIATION_API_ID")=(open."MESSAGE_VARIATION_API_ID")
OR
(send."CANVAS_STEP_API_ID")=(open."CANVAS_STEP_API_ID"))
LEFT JOIN users_messages_pushnotification_bounce_shared AS bounce ON (send."USER_ID")=(bounce."USER_ID")
AND
(send."DEVICE_ID")=(bounce."DEVICE_ID")
AND
((send."MESSAGE_VARIATION_API_ID")=(bounce."MESSAGE_VARIATION_API_ID")
OR
(send."CANVAS_STEP_API_ID")=(bounce."CANVAS_STEP_API_ID"))
LIMIT 500;
You can use this daily email messaging cadence query to analyze the time between emails that a user receives.
For example, if a user received two emails in one day, they would fall under 0 "days since last received". If they received one email on Monday and one on Tuesday, they would fall into the 1 "days since last received" cohort.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
WITH email_messaging_cadence AS (WITH deliveries AS
(SELECT TO_TIMESTAMP(time) AS delivered_timestamp,
email_address AS delivered_address,
message_variation_api_id AS d_message_variation_api_id,
canvas_step_api_id AS d_canvas_step_api_id,
campaign_api_id AS d_campaign_api_id,
canvas_api_id AS d_canvas_api_id,
id AS delivered_id,
rank() over (partition by delivered_address ORDER BY delivered_timestamp ASC) AS delivery_event,
min(delivered_timestamp) over (partition by delivered_address ORDER BY delivered_timestamp ASC) AS first_delivered,
datediff(day, lag(delivered_timestamp) over (partition by delivered_address ORDER BY delivered_timestamp ASC), delivered_timestamp) AS diff_days,
datediff(week, lag(delivered_timestamp) over (partition by delivered_address ORDER BY delivered_timestamp ASC), delivered_timestamp) AS diff_weeks
from USERS_MESSAGES_EMAIL_DELIVERY_SHARED GROUP BY 1,2,3,4,5,6,7), opens AS
(SELECT DISTINCT email_address AS open_address,
message_variation_api_id AS o_message_variation_api_id,
canvas_step_api_id AS o_canvas_step_api_id
FROM USERS_MESSAGES_EMAIL_OPEN_SHARED), clicks AS
(SELECT DISTINCT email_address AS click_address,
message_variation_api_id AS c_message_variation_api_id,
canvas_step_api_id AS c_canvas_step_api_id
FROM USERS_MESSAGES_EMAIL_CLICK_SHARED) SELECT * FROM deliveries
LEFT JOIN opens
ON (deliveries.delivered_address)=(opens.open_address)
AND ((deliveries.d_message_variation_api_id)=(opens.o_message_variation_api_id) OR (deliveries.d_canvas_step_api_id)=(opens.o_canvas_step_api_id))
LEFT JOIN clicks
ON (deliveries.delivered_address)=(clicks.click_address)
AND ((deliveries.d_message_variation_api_id)=(clicks.c_message_variation_api_id) OR (deliveries.d_canvas_step_api_id)=(clicks.c_canvas_step_api_id))
)
SELECT
email_messaging_cadence."DIFF_DAYS" AS "email_messaging_cadence.days_since_last_received",
(count(distinct email_messaging_cadence."OPEN_ADDRESS", email_messaging_cadence."O_MESSAGE_VARIATION_API_ID")
+count(distinct email_messaging_cadence."OPEN_ADDRESS", email_messaging_cadence."O_CANVAS_STEP_API_ID"))/(COUNT(DISTINCT email_messaging_cadence."DELIVERED_ID" )) AS "email_messaging_cadence.unique_open_rate"
FROM email_messaging_cadence GROUP BY 1
ORDER BY 1
LIMIT 500;
You can use this unique email clicks query to analyze the unique email click in a given time window. The algorithm to calculate this is as follows:
- Partition the events by the key (
app_group_id,message_variation_id,dispatch_id,email_address). - In each partition, order the events by time, and the first event is always a unique event.
- For every subsequent event, if it occurred more than seven days after its predecessor, is considered a unique event.
We can use Snowflake’s windowing functions to help us achieve this. The following query gives us all email clicks in the last 365 days and indicates which events are unique in the is_unique column:
1
2
3
4
5
6
7
8
9
SELECT id, app_group_id, message_variation_api_id, dispatch_id, email_address, time,
ROW_NUMBER() OVER (PARTITION BY app_group_id, message_variation_api_id, dispatch_id, email_address order by time) row_number,
LAG(time, 1, time) OVER (PARTITION BY app_group_id, message_variation_api_id, dispatch_id, email_address order by time) previous_time,
time - previous_time AS diff,
IFF(row_number = 1, true, IFF(diff >= 7*24*3600, true, false)) AS is_unique
FROM USERS_MESSAGES_EMAIL_CLICK_SHARED
WHERE
time < DATE_PART('EPOCH_SECOND', TO_TIMESTAMP(CURRENT_TIMESTAMP()))
AND time > DATE_PART('EPOCH_SECOND', TO_TIMESTAMP(CURRENT_TIMESTAMP())) - 365*24*3600;
If you just want to see the unique events, use the QUALIFY clause:
1
2
3
4
5
6
7
8
9
10
SELECT id, app_group_id, message_variation_api_id, dispatch_id, email_address, time,
ROW_NUMBER() OVER (PARTITION BY app_group_id, message_variation_api_id, dispatch_id, email_address order by time) row_number,
LAG(time, 1, time) OVER (PARTITION BY app_group_id, message_variation_api_id, dispatch_id, email_address order by time) previous_time,
time - previous_time AS diff,
IFF(row_number = 1, true, IFF(diff >= 7*24*3600, true, false)) AS is_unique
FROM USERS_MESSAGES_EMAIL_CLICK_SHARED
WHERE
time < DATE_PART('EPOCH_SECOND', TO_TIMESTAMP(CURRENT_TIMESTAMP()))
AND time > DATE_PART('EPOCH_SECOND', TO_TIMESTAMP(CURRENT_TIMESTAMP())) - 365*24*3600
QUALIFY is_unique = true;
To further see unique event counts grouped by email address:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
WITH unique_events AS(
SELECT id, app_group_id, message_variation_api_id, dispatch_id, email_address, time,
ROW_NUMBER() OVER (PARTITION BY app_group_id, message_variation_api_id, dispatch_id, email_address order by time) row_number,
LAG(time, 1, time) OVER (PARTITION BY app_group_id, message_variation_api_id, dispatch_id, email_address order by time) previous_time,
time - previous_time AS diff,
IFF(row_number = 1, true, iff(diff >= 7*24*3600, true, false)) AS is_unique
FROM USERS_MESSAGES_EMAIL_CLICK_SHARED
WHERE
time < DATE_PART('EPOCH_SECOND', TO_TIMESTAMP(CURRENT_TIMESTAMP()))
AND time > DATE_PART('EPOCH_SECOND', TO_TIMESTAMP(CURRENT_TIMESTAMP())) - 365*24*3600
QUALIFY is_unique = true)
SELECT email_address, count(*) AS count
FROM unique_events
GROUP BY email_address;
Use this query to approximate Unique Opens from Snowflake email open events—for example, to reconcile with the Unique Opens column in the dashboard.
This example returns three counts:
- Unique Opens (over 7 days): Unique opens over a rolling seven-day period.
- Unique Opens (during date window): Unique opens within the given time period. This is regardless of any opens that occurred prior to the time period.
- Unique Opens (for emails delivered within same timeframe): Unique opens where the paired delivery event also occurred inside the same window (useful when you only want opens tied to messages delivered in that period).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
Set or comment out variables if not required. These are set per session.
You can obtain the from and to dates from the Campaign/Canvas/Canvas step URL. These are the startDate and endDate parameters.
For example, endDate=1656799199&startDate=1656194400
To run, select all of this code block (CMD + A) and run to first set the necessary variables and run the SELECT statements below.
*/
SET fromDateTime = '1656194400';
SET toDateTime = '1656799199';
-- SET campaignID = '';
-- SET canvasID = '';
SET canvasStepID = '61b0a249745a0c5ac67a11d3';
SELECT
'Unique Opens (over 7 days)' metric, COUNT(DISTINCT(user_id, dispatch_id)) total
FROM
users_messages_email_open_shared
WHERE
/* Comment out where not required */
-- campaign_id = $campaignID AND
-- canvas_id = $canvasID AND
canvas_step_id = $canvasStepID AND
time BETWEEN $fromDateTime and $toDateTime AND
not exists (select
umeo.user_id
from
users_messages_email_open_shared umeo
where
umeo.user_id = users_messages_email_open_shared.user_id and
umeo.canvas_step_id = users_messages_email_open_shared.canvas_step_id and
to_timestamp(umeo.time) between dateadd(day, -7, to_timestamp(users_messages_email_open_shared.time)) and dateadd(second, -1, to_timestamp(users_messages_email_open_shared.time)))
UNION
SELECT
'Unique Opens (during date window)' metric, COUNT(DISTINCT(user_id, dispatch_id)) total
FROM
users_messages_email_open_shared
WHERE
/* Comment out where not required */
-- campaign_id = $campaignID AND
-- canvas_id = $canvasID AND
canvas_step_id = $canvasStepID AND
time BETWEEN $fromDateTime and $toDateTime
UNION
SELECT
'Unique Opens (for emails delivered within same timeframe)' metric, COUNT(DISTINCT(user_id, dispatch_id)) total
FROM
users_messages_email_open_shared
WHERE
/* Comment out where not required */
-- campaign_id = $campaignID AND
-- canvas_id = $canvasID AND
canvas_step_id = $canvasStepID AND
time BETWEEN $fromDateTime and $toDateTime AND
EXISTS (select user_id
from users_messages_email_delivery_shared umed
where
umed.user_id = users_messages_email_open_shared.user_id and
umed.dispatch_id = users_messages_email_open_shared.dispatch_id and
umed.time between $fromDateTime and $toDateTime);