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(...) and WHERE 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, and GENDER
  • 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.

Did this answer your question?
๐Ÿ˜ž
๐Ÿ˜
๐Ÿคฉ

Last updated on August 6, 2021