Skip to content

Instantly share code, notes, and snippets.

@thomashandorf
Created January 7, 2024 20:09
Show Gist options
  • Select an option

  • Save thomashandorf/46dbc5a548086378bf7dbcaba24d4700 to your computer and use it in GitHub Desktop.

Select an option

Save thomashandorf/46dbc5a548086378bf7dbcaba24d4700 to your computer and use it in GitHub Desktop.
Joining incomplete data with COALESCE and IFNULL
SELECT
COALESCE(a.date, s.date) as date,
COALESCE(a.campaign, s.campaign) as campaign,
IFNULL(a.spent, 0) as spent,
IFNULL(a.impressions, 0) as impressions,
IFNULL(a.clicks, 0) as clicks,
IFNULL(s.sessions, 0) as sessions,
IFNULL(s.conversions, 0) as conversions
FROM
Ads a
FULL OUTER JOIN
Session s
ON
a.date = s.date AND a.campaign = s.campaign
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment