surfacecleaningcoalesce_smart
Cleaningaggregate · returns varchar

COALESCE_SMART

Pick the best non-null value from a group (quality-aware COALESCE)

Per-group — reads the whole group in one call.

cleaningllmtext

Syntax

COALESCE_SMART({{ values }})
COALESCE_SMART({{ values }}, '{{ preference }}')

Arguments

nametypedescription
valuesVARCHARValues to coalesce
preference(optional)VARCHARPreference hint: 'prefer mobile', 'prefer complete', etc.

About

Like SQL's COALESCE but picks the best non-null value in the group, not just the first one. Quality beats position order. Useful when reconciling a column across multiple sources where the "first available" value isn't necessarily the best: a customer name that's truncated in one source and fully formed in another, an address that's abbreviated in one feed and complete in another, a description that's empty-but-not-NULL in a legacy export. Pass an optional preference string to steer what "best" means (`'most complete'`, `'most recent'`, `'most formal'`); default is highest overall quality. For scalar null-fallback with a constant, stick with SQL's COALESCE — it's free. Use COALESCE_SMART when you're collapsing a group of rows and want the operator to evaluate the candidates.

Examples

Picks best non-null value, skipping fake emails

WITH
  test_data AS (
    SELECT
      *
    FROM
      (
        VALUES
          (NULL),
          ('N/A'),
          ('test@test.com'),
          ('jane@company.com')
      ) AS t (email)
  )
SELECT
  COALESCE_SMART (email)
FROM
  test_data

Nearby rabbit holes

same domain
Climb back to The Looking Glass