Snowflake Share: example queries
Ready-to-run queries for Snowflake Share. Each one answers a common question. Copy one into a Snowflake worksheet and run it.
Before you run a query
Select a warehouse and the database first. The examples below use short table names like v3.attribution.
USE WAREHOUSE <your_warehouse>;
USE DATABASE dreamdata_platform;Or skip USE DATABASE and write full table names, such as dreamdata_platform.v3.attribution.
Your stage names may differ
The examples use the stages MQL and NewBiz and the Data-Driven attribution model. Yours come from your Data Hub setup. Each query has a comment where to change them.
To list your stage names:
SELECT DISTINCT stage_name
FROM v3.stages
ORDER BY stage_name;All timestamps are in UTC.
For marketers
Which channels created the most MQLs last month?
A weight, from 0 to 1, is a session's share of credit for a stage. The attributed_mqls column adds up the weights. The attributed_value column adds up weight times stage value.
SELECT a.session:channel::STRING AS channel,
SUM(attr.value:weight::FLOAT) AS attributed_mqls,
SUM(attr.value:value::FLOAT) AS attributed_value
FROM v3.attribution a,
-- FLATTEN expands the attribution array into one row per model.
LATERAL FLATTEN(input => a.attribution) attr
-- Replace 'MQL' with your stage name if it differs.
WHERE a.stage:name::STRING = 'MQL'
-- Last full calendar month.
-- Stage timestamps are text ending in " UTC", so the suffix is removed first.
AND TO_TIMESTAMP_NTZ(REPLACE(a.stage:timestamp::STRING, ' UTC', ''))
>= DATE_TRUNC(month, DATEADD(month, -1, SYSDATE()::DATE))
AND TO_TIMESTAMP_NTZ(REPLACE(a.stage:timestamp::STRING, ' UTC', ''))
< DATE_TRUNC(month, SYSDATE()::DATE)
-- Replace 'Data-Driven' to use a different attribution model.
AND attr.value:model::STRING = 'Data-Driven'
GROUP BY channel
ORDER BY attributed_mqls DESC;Which channels and sources drove new business revenue last month?
For pipeline by channel, replace 'NewBiz' with your pipeline stage.
SELECT a.session:channel::STRING AS channel,
a.session:source::STRING AS source,
SUM(attr.value:value::FLOAT) AS attributed_value,
SUM(attr.value:weight::FLOAT) AS attributed_new_biz
FROM v3.attribution a,
LATERAL FLATTEN(input => a.attribution) attr
-- Replace 'NewBiz' with your stage name if it differs.
WHERE a.stage:name::STRING = 'NewBiz'
AND TO_TIMESTAMP_NTZ(REPLACE(a.stage:timestamp::STRING, ' UTC', ''))
>= DATE_TRUNC(month, DATEADD(month, -1, SYSDATE()::DATE))
AND TO_TIMESTAMP_NTZ(REPLACE(a.stage:timestamp::STRING, ' UTC', ''))
< DATE_TRUNC(month, SYSDATE()::DATE)
-- Replace 'Data-Driven' to use a different attribution model.
AND attr.value:model::STRING = 'Data-Driven'
GROUP BY channel, source
ORDER BY attributed_value DESC;How does ad spend compare with attributed revenue, per channel, last quarter?
Deals often close months after the spend, so this compares a full quarter. Spend is in the currency set in Dreamdata Platform. To compare spend with pipeline instead, replace 'NewBiz' with a pipeline stage.
WITH spend AS (
SELECT channel,
SUM(cost) AS spend
FROM v3.spend
WHERE timestamp >= DATE_TRUNC(quarter, DATEADD(quarter, -1, SYSDATE()::DATE))
AND timestamp < DATE_TRUNC(quarter, SYSDATE()::DATE)
GROUP BY channel
),
revenue AS (
SELECT a.session:channel::STRING AS channel,
SUM(attr.value:value::FLOAT) AS attributed_value
FROM v3.attribution a,
LATERAL FLATTEN(input => a.attribution) attr
-- Replace 'NewBiz' with your stage name if it differs.
WHERE a.stage:name::STRING = 'NewBiz'
AND TO_TIMESTAMP_NTZ(REPLACE(a.stage:timestamp::STRING, ' UTC', ''))
>= DATE_TRUNC(quarter, DATEADD(quarter, -1, SYSDATE()::DATE))
AND TO_TIMESTAMP_NTZ(REPLACE(a.stage:timestamp::STRING, ' UTC', ''))
< DATE_TRUNC(quarter, SYSDATE()::DATE)
-- Replace 'Data-Driven' to use a different attribution model.
AND attr.value:model::STRING = 'Data-Driven'
GROUP BY channel
)
SELECT COALESCE(r.channel, s.channel) AS channel,
s.spend,
r.attributed_value,
r.attributed_value / NULLIF(s.spend, 0) AS revenue_per_spend
FROM revenue r
FULL OUTER JOIN spend s ON s.channel = r.channel
ORDER BY s.spend DESC NULLS LAST;How many touchpoints did each channel get in the last 30 days?
One event can have several rows, one for each of its contact's companies. COUNT(DISTINCT dd_event_activity_id) counts each event once. Impressions have no activity ID, so they're left out.
SELECT session:channel::STRING AS channel,
COUNT(DISTINCT dd_event_activity_id) AS touchpoints
FROM v3.events
WHERE timestamp >= DATEADD(day, -30, SYSDATE()::DATE)
GROUP BY channel
ORDER BY touchpoints DESC;For sales
How many companies reached each stage this month?
SELECT stage_name,
COUNT(DISTINCT dd_company_id) AS companies
FROM v3.stages
WHERE timestamp >= DATE_TRUNC(month, SYSDATE()::DATE)
AND dd_company_id IS NOT NULL
GROUP BY stage_name
ORDER BY companies DESC;Which companies reached a stage this month, and what touched them?
One row per company, with its touchpoints in the 90 days before it reached the stage and its top channel.
WITH reached AS (
SELECT dd_company_id,
MIN(timestamp) AS reached_at
FROM v3.stages
-- Replace 'NewBiz' with the stage you want.
WHERE stage_name = 'NewBiz'
AND timestamp >= DATE_TRUNC(month, SYSDATE()::DATE)
AND dd_company_id IS NOT NULL
GROUP BY dd_company_id
),
touches AS (
SELECT r.dd_company_id,
e.session:channel::STRING AS channel,
COUNT(DISTINCT e.dd_event_activity_id) AS touchpoints
FROM reached r
JOIN v3.events e
ON e.dd_company_id = r.dd_company_id
AND e.timestamp < r.reached_at
AND e.timestamp >= DATEADD(day, -90, r.reached_at)
GROUP BY r.dd_company_id, channel
)
SELECT c.domain,
r.reached_at,
COALESCE(SUM(t.touchpoints), 0) AS touchpoints,
MAX_BY(t.channel, t.touchpoints) AS top_channel
FROM reached r
JOIN v3.companies c ON c.dd_company_id = r.dd_company_id
LEFT JOIN touches t ON t.dd_company_id = r.dd_company_id
GROUP BY r.dd_company_id, c.domain, r.reached_at
ORDER BY r.reached_at DESC;Which channels touched the companies that reached a stage this month?
This counts touchpoints in the 90 days before each company reached the stage.
WITH reached AS (
SELECT dd_company_id,
MIN(timestamp) AS reached_at
FROM v3.stages
-- Replace 'NewBiz' with the stage you want.
WHERE stage_name = 'NewBiz'
AND timestamp >= DATE_TRUNC(month, SYSDATE()::DATE)
AND dd_company_id IS NOT NULL
GROUP BY dd_company_id
)
SELECT e.session:channel::STRING AS channel,
COUNT(DISTINCT e.dd_event_activity_id) AS touchpoints,
COUNT(DISTINCT e.dd_company_id) AS companies
FROM v3.events e
JOIN reached r ON r.dd_company_id = e.dd_company_id
WHERE e.timestamp < r.reached_at
AND e.timestamp >= DATEADD(day, -90, r.reached_at)
GROUP BY channel
ORDER BY touchpoints DESC;For ops and data teams
Is my data up to date?
Shows the latest activity in each table, such as the newest touchpoint or ad spend. This is not the load time, so a quiet channel can look old. Data loads once a day. If every date is much older than you expect, check your Data modelling schedule.
SELECT 'events' AS table_name, MAX(timestamp) AS newest FROM v3.events
UNION ALL SELECT 'spend', MAX(timestamp) FROM v3.spend
UNION ALL SELECT 'stages', MAX(timestamp) FROM v3.stages
ORDER BY newest DESC;How do I join Dreamdata data with my own data?
Join on the company domain. This example counts, per stage, the companies that reached it this month and are also in your own accounts table. Replace my_db.my_schema.my_accounts and its domain column with yours.
SELECT s.stage_name,
COUNT(DISTINCT s.dd_company_id) AS companies
FROM v3.stages s
JOIN v3.companies c ON c.dd_company_id = s.dd_company_id
JOIN my_db.my_schema.my_accounts m ON LOWER(m.domain) = LOWER(c.domain)
WHERE s.timestamp >= DATE_TRUNC(month, SYSDATE()::DATE)
GROUP BY s.stage_name
ORDER BY companies DESC;