Sub topic 4
Writing segment rules
This chapter assumes you know AND, OR, and NOT, and have used an email service provider or CRM. The SQL examples use PostgreSQL and are optional. A segment builder in a provider's interface expresses the same ideas with dropdowns.
What a rule is
A segment rule is a test that each subscriber either passes or fails. Everyone who passes is in the segment. Every rule has three parts:
| Part | Question it answers | Example |
|---|---|---|
| Condition | What must be true about the subscriber? | `event_type = 'email_click'` |
| Time window | When must it have happened? | in the last 30 days |
| Logic | How do several conditions combine? | AND, OR, NOT |
Most rule mistakes come from one of these three parts being vague or misread.
Conditions
A condition compares a profile field, an event, or a tag to a value. Profile conditions test the current state, such as status = 'subscribed'. Event conditions test whether something happened, such as a click or a purchase. Event conditions need a time window, because "ever clicked" stops meaning anything after a year.
Boolean logic and grouping
AND narrows a segment and OR widens it. NOT removes people. When you mix AND and OR, group them with parentheses. Most query languages evaluate AND before OR, and some segment builders group conditions differently from how you read them aloud.
This rule selects subscribed contacts who clicked or purchased in the last 30 days, and removes anyone who complained in the last 14 days:
-- PostgreSQL: recently active subscribers, excluding recent complaints
SELECT s.id
FROM subscribers s
WHERE s.status = 'subscribed'
AND (
EXISTS (
SELECT 1
FROM events e
WHERE e.subscriber_id = s.id
AND e.event_type = 'email_click'
AND e.occurred_at >= now() - interval '30 days'
)
OR s.last_purchase_at >= now() - interval '30 days'
)
AND NOT EXISTS (
SELECT 1
FROM events e
WHERE e.subscriber_id = s.id
AND e.event_type = 'support_complaint'
AND e.occurred_at >= now() - interval '14 days'
);Without the parentheses around the OR, the purchase condition would sit outside the AND chain. Subscribers who purchased but had unsubscribed could then slip in.
Time windows
There are two common kinds of window:
- A rolling window is relative to now, such as
now() - interval '30 days'. Membership changes daily as events age out. - A fixed window uses set dates, such as "between 1 and 30 November". Membership only changes if new events arrive inside it.
Use rolling windows for ongoing automation and fixed windows for reporting on a past campaign. Pick the window length from your sending cadence. If you send weekly, a 7-day window leaves a subscriber one chance to qualify. A 30-day window gives about four.
Common errors
Missing values and NOT
In SQL, comparing a missing value (NULL) to anything gives "unknown", and rows with unknown results are dropped. A negated condition therefore silently excludes people who have no data:
-- Wrong: never-purchased subscribers have NULL and vanish from the result
SELECT id
FROM subscribers
WHERE NOT (last_purchase_at >= now() - interval '90 days');
-- Right: handle the missing value explicitly
SELECT id
FROM subscribers
WHERE last_purchase_at IS NULL
OR last_purchase_at < now() - interval '90 days';Many segment builders have the same trap. A condition like "last purchase is not in the last 90 days" may or may not include people who never purchased. Test it on a known contact.
Other mistakes
- Vague windows: a rule with no time limit grows forever and drifts away from current behaviour.
- Unnamed assumptions: "engaged" can mean opened, clicked, or purchased. Write down which one.
- Over-stacking: each AND shrinks the segment. Check the count after each added condition.
- Trusting opens alone: some mail clients prefetch images, so an open may not reflect a human reading. Prefer clicks and purchases where you can.
A habit for writing rules
- State the goal and message in one sentence.
- List the conditions as plain statements.
- Add a time window to every event condition.
- Add the consent check and any exclusions.
- Run the rule, check the count, and spot-check a few members and a few non-members.
Exercise
A rule reads "status = subscribed AND clicked in the last 30 days OR purchased in the last 30 days". What can go wrong, and how do you fix it?
Show answer
Because AND is evaluated before OR, the rule means "(subscribed AND clicked) OR purchased". Purchasers who have unsubscribed would be included. Add parentheses so it reads "subscribed AND (clicked in the last 30 days OR purchased in the last 30 days)", which keeps the consent check outside the OR group.
Exercise
You want everyone who has not purchased in 90 days, including people who never purchased. Why does NOT (last_purchase_at >= now() - interval '90 days') miss some of them?
Show answer
Never-purchased subscribers have a NULL lastpurchaseat. The comparison returns unknown, and NOT unknown is still unknown, so those rows are dropped. Use last_purchase_at IS NULL OR last_purchase_at < now() - interval '90 days'.