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;