Skip to content

Instantly share code, notes, and snippets.

@Mr--John-Doe
Last active June 12, 2022 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mr--John-Doe/346c7d60f9c84afc6d02cd233267decf to your computer and use it in GitHub Desktop.
Save Mr--John-Doe/346c7d60f9c84afc6d02cd233267decf to your computer and use it in GitHub Desktop.
WITH
union_tab as (
select username, 'daily_activity' as label, value from web.daily_activity UNION ALL
select username, 'user_address' as label, value from web.user_attributes_1 UNION ALL
select username, 'user_address' as label, value from web.user_attributes_1 UNION ALL
select username, 'user_mobile' as label, value from web.user_attributes_2 UNION ALL
select username, 'user_email_optin' as label, value from web.user_attributes_3 UNION ALL
select username, 'user_mobile_optin' as label, value from web.user_attributes_4 UNION ALL
(... and so on ...)
)
SELECT username
, "'daily_activity'".value:date AS date
, "'daily_activity'".value:hit_count AS hit_count
, "'user_address'".value:user_address AS user_address
, "'user_mobile'".value:user_mobile AS user_mobile
, "'user_email_optin'".value:user_email_optin AS user_email_optim
, "'user_email_optin'".value:optin_date AS user_email_optin_date
, "'user_mobile_optin'".value:mobile AS user_mobile_optin
( ... )
FROM union_tab
PIVOT (
MAX(value)
FOR label IN (
'daily_activity',
'user_address',
'user_mobile',
'user_email_optin',
'user_mobile_optin',
( ... )
)
)
WHERE date IS NOT NULL -- main left join table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment