SQL Templates
๐ง Data Normalization Templates (AudienceSync)
This guide walks you through two templates for exploding comma-separated fields inside AudienceSync. Both templates help you normalize messy audience data for upload to platforms like Facebook Ads, TikTok, or CRMs.
Note: Make sure to leave no unnecessary spaces when pasting in the queries.
๐ฆ Template 1: Explode Values Template
Use this when:
You have a single column with comma-separated values (e.g., hash1,hash2,hash3
) and want to split it into multiple rows, one value per row.
โ How It Works
This template:
- Uses
STRING_SPLIT(..., ',')
to break values into an array.
- Uses
UNNEST(...)
to explode them into rows.
- Uses
TRIM(...)
andWHERE
filters to clean empty/blank values.
๐งช Example Input
SHA256_PERSONAL_EMAIL
hash1,hash2,hash3
๐ค Output
sha256_personal_email
hash1
hash2
hash3
โ Copy & Paste Version (for AudienceSync)
SELECT
TRIM(value) AS sha256_personal_email
FROM (
SELECT
UNNEST(STRING_SPLIT(SHA256_PERSONAL_EMAIL, ',')) AS value
FROM all_sources
)
WHERE value IS NOT NULL
AND value <> '';
๐ Replace SHA256_PERSONAL_EMAIL with your column name
๐ฆ Template 2: Facebook Expansion
Use this when:
You have multiple fields (e.g., SHA256_PERSONAL_EMAIL
, MOBILE_PHONE
, etc.) containing comma-separated values, and you need:
- One value per row
- All emails collapsed into a single field
- All phone numbers collapsed into a single field
- Static fields (like
FIRST_NAME
) repeated for each row
๐ How It Works
- Splits each multi-value field using
split(...)
- Generates an index list using
range(...)
- Explodes the list with
UNNEST(...)
- Combines aligned values using
idx
- Uses
COALESCE(...)
to select the first non-empty email or phone
- Retains static fields without splitting
๐งช Example Input
SHA256_PERSONAL_EMAIL | SHA256_BUSINESS_EMAIL | MOBILE_PHONE | FIRST_NAME |
hash1,hash2 | hash3 | 1111,2222,3333 | Alice |
๐ค Output
normal_email | normal_phone | FIRST_NAME |
hash1 | 1111 | Alice |
hash2 | 2222 | Alice |
hash3 | 3333 | Alice |
โ Copy & Paste Version (for AudienceSync)
SELECT
-- Combined Email Field
COALESCE(
NULLIF(TRIM(split_sha256[idx]), ''),
NULLIF(TRIM(split_sha256_business_email[idx]), '')
) AS normal_email,
-- Combined Phone Field
COALESCE(
NULLIF(TRIM(split_mobile_phone[idx]), ''),
NULLIF(TRIM(split_personal_phone[idx]), ''),
NULLIF(TRIM(split_direct_number[idx]), '')
) AS normal_phone,
-- Static fields (not exploded)
FIRST_NAME,
LAST_NAME,
PERSONAL_CITY,
PERSONAL_STATE,
PERSONAL_ZIP,
GENDER
FROM (
SELECT
-- Splitting multi-value fields
split(SHA256_PERSONAL_EMAIL, ',') AS split_sha256,
split(PERSONAL_EMAILS, ',') AS split_emails,
split(DIRECT_NUMBER, ',') AS split_direct_number,
split(MOBILE_PHONE, ',') AS split_mobile_phone,
split(PERSONAL_PHONE, ',') AS split_personal_phone,
split(SHA256_BUSINESS_EMAIL, ',') AS split_sha256_business_email,
-- Static fields passed through
FIRST_NAME,
LAST_NAME,
PERSONAL_CITY,
PERSONAL_STATE,
PERSONAL_ZIP,
GENDER,
-- Index list for row explosion
range(
0,
GREATEST(
array_length(split(SHA256_PERSONAL_EMAIL, ',')),
array_length(split(PERSONAL_EMAILS, ',')),
array_length(split(DIRECT_NUMBER, ',')),
array_length(split(MOBILE_PHONE, ',')),
array_length(split(PERSONAL_PHONE, ',')),
array_length(split(SHA256_BUSINESS_EMAIL, ','))
)
) AS idx_list
FROM all_sources
) t,
UNNEST(idx_list) AS idx_struct(idx)
WHERE (
(split_sha256[idx] IS NOT NULL AND split_sha256[idx] <> '')
OR (split_sha256_business_email[idx] IS NOT NULL AND split_sha256_business_email[idx] <> '')
)
AND (
(split_mobile_phone[idx] IS NOT NULL AND split_mobile_phone[idx] <> '')
OR (split_personal_phone[idx] IS NOT NULL AND split_personal_phone[idx] <> '')
OR (split_direct_number[idx] IS NOT NULL AND split_direct_number[idx] <> '')
);
๐ฏ Why this works
This template:
- Combines SHA256_PERSONAL_EMAIL and SHA256_BUSINESS_EMAIL into a single column called
normal_email
- Merges MOBILE_PHONE, PERSONAL_PHONE, and DIRECT_NUMBER into a single
normal_phone
- Retains static fields like
FIRST_NAME
,ZIP
, andGENDER
- Ensures one clean value per row โ exactly what Facebook, TikTok, and Google expect
๐ง Template 3: State and Field Selection Template (AudienceSync)
This template shows you how to select specific fields based on geography or attribute filters, like PERSONAL_STATE = 'NY'
.
Itโs designed for simple but powerful selections inside AudienceSync.
๐ฆ Template 3: State and Field Selection
Use this when:
You need to pull a specific subset of records based on a filter condition like "State = NY", "State = CA", or any other field match.
โ How It Works
- Pick the fields you want to SELECT (e.g., emails, names, phones).
- Use a simple
WHERE
clause to filter by state, gender, or any attribute.
- AudienceSync will return only matching rows โ clean and ready for export or processing.
โ Copy & Paste Version (for AudienceSync)
SELECT
FIELD1,
FIELD2,
FIELD3
FROM all_sources
WHERE FILTER_COLUMN = 'your_filter_value';
๐ Replace FIELD1, FIELD2, FILTER_COLUMN, and 'your_filter_value'
๐ Example 1: Select Emails and Names Where State is NY
Goal:
Pull PERSONAL_EMAILS
, FIRST_NAME
, LAST_NAME
only for users in New York.
๐งช Example Input
PERSONAL_EMAILS | FIRST_NAME | LAST_NAME | PERSONAL_STATE |
alice@example.com | Alice | Johnson | NY |
bob@example.com | Bob | Smith | CA |
dave@example.com | Dave | Wilson | NY |
๐ค Output
PERSONAL_EMAILS | FIRST_NAME | LAST_NAME |
alice@example.com | Alice | Johnson |
dave@example.com | Dave | Wilson |
โ Copy & Paste Version (for AudienceSync)
SELECT
PERSONAL_EMAILS,
FIRST_NAME,
LAST_NAME
FROM all_sources
WHERE PERSONAL_STATE = 'NY';
๐ฏ Why this is useful
- Build New York-only audience lists.
- Prep for geographically targeted outreach.
๐ Example 2: Select Mobile Phones Where State is CA
Goal:
Pull MOBILE_PHONE
and FIRST_NAME
only for users in California.
๐งช Example Input
MOBILE_PHONE | FIRST_NAME | PERSONAL_STATE |
1234567890 | Alice | CA |
0987654321 | Bob | NY |
5551234567 | Carol | CA |
๐ค Output
MOBILE_PHONE | FIRST_NAME |
1234567890 | Alice |
5551234567 | Carol |
โ Copy & Paste Version (for AudienceSync)
SELECT
MOBILE_PHONE,
FIRST_NAME
FROM all_sources
WHERE PERSONAL_STATE = 'CA';
๐ฏ Why this is useful
- Prepare California-focused SMS campaigns.
- Quickly segment users by state for regional marketing pushes.
Last updated on August 6, 2021