Data Views in SQL: who opened or clicked in the last 30 days
Build an engaged audience with the _Open and _Click Data Views in a Query Activity, accounting for the time zone and the six-month limit.
Data Views are system tables in Marketing Cloud Engagement that hold the history of sends, opens, clicks, bounces and unsubscribes. They do not show up in Contact Builder, but you can query them with SQL in a Query Activity (Automation Studio). With them you can build engagement audiences without relying on reports.
The most used Data Views
| Data View | What it stores |
|---|---|
_Sent |
Every email sent to every subscriber |
_Open |
Opens |
_Click |
Link clicks |
_Bounce |
Bounces, with the reason |
_Unsubscribe |
Unsubscribes |
_Job |
The sends (email name, date, sender) |
The columns that tie everything together are SubscriberKey, JobID and EventDate.
Two details that break many queries
- Six months of history. Data Views keep roughly six months of events. For longer analyses, copy the data into your own Data Extension on a schedule.
- Time zone.
EventDateis stored in Marketing Cloud server time (Central Standard Time, UTC−6, with no daylight saving). For "the last 30 days" this barely matters; for "yesterday, midnight to 11:59 p.m. in my time zone", it does.
The query: engaged in the last 30 days
The goal is an audience of everyone who opened or clicked any email in the last 30 days, with the date of their latest engagement.
SELECT
e.SubscriberKey,
MAX(e.EventDate) AS LastEngagement
FROM (
SELECT o.SubscriberKey, o.EventDate
FROM _Open o
WHERE o.EventDate >= DATEADD(DAY, -30, GETDATE())
UNION ALL
SELECT c.SubscriberKey, c.EventDate
FROM _Click c
WHERE c.EventDate >= DATEADD(DAY, -30, GETDATE())
) e
GROUP BY e.SubscriberKeyWhy it is written this way:
UNION ALLcombines opens and clicks without paying for duplicate removal at this stage;- the final
GROUP BYleaves one row per subscriber, which is required ifSubscriberKeyis the primary key of the target Data Extension; MAX(EventDate)keeps the most recent date, useful for segments like "engaged in the last 7 days".
Setting up the Query Activity
- Create the target Data Extension
Engaged_30dwithSubscriberKey(Text, primary key) andLastEngagement(Date). - In the Query Activity, pick that Data Extension as the target and the Overwrite action: the audience is rebuilt from scratch on every run.
- Put the Query Activity in a daily automation.
Useful variations
Clickers only (a stronger signal than opens, which are inflated today by email client privacy features):
SELECT c.SubscriberKey, MAX(c.EventDate) AS LastClick
FROM _Click c
WHERE c.EventDate >= DATEADD(DAY, -30, GETDATE())
GROUP BY c.SubscriberKeyInactive subscribers: everyone who received an email in the last 90 days but did not open or click. Start from
_Sent and use NOT EXISTS against _Open and _Click.
Summary
Data Views give you engagement history directly in SQL. Remember the six-month retention and the CST time zone, keep one row per subscriber in the result, and use Overwrite for audiences that are rebuilt every day.
Related
| Title | Type | Platform | Belt | Reading |
|---|---|---|---|---|
| Deduplicate records with ROW_NUMBER() in a Query Activity | Snippet | Marketing Cloud Engagement | Green | 2 min |
| AttributeValue and empty fields: AMPscript personalization that never breaks a send | Tutorial | Marketing Cloud Engagement | White | 3 min |
| How to use LookupRows in AMPscript (and when to switch to Lookup or LookupOrderedRows) | Tutorial | Marketing Cloud Engagement | Yellow | 3 min |
| Marketing Cloud REST API: OAuth 2.0 authentication and writing to a Data Extension | Tutorial | Marketing Cloud Engagement | Brown | 3 min |