Skip to content

Instantly share code, notes, and snippets.

@danabrey
Last active July 15, 2018 11:14
Show Gist options
  • Save danabrey/8b2bc6c4783fe8ddfaf8a2be4ac51b3c to your computer and use it in GitHub Desktop.
Save danabrey/8b2bc6c4783fe8ddfaf8a2be4ac51b3c to your computer and use it in GitHub Desktop.
SELECT uts.user_id, MIN(uts.ts) as earliest_action FROM (
SELECT user_id, MIN(placed) as ts FROM ADS WHERE user_id > 0 GROUP BY user_id UNION ALL
SELECT user_id, MIN(placed_on) as ts FROM reviews WHERE user_id > 0 GROUP BY user_id UNION ALL
SELECT EVENTS_user_xref.user_id as user_id, MIN(EVENTS.placed_time) as ts FROM EVENTS JOIN EVENTS_user_xref ON EVENTS.id = EVENTS_user_xref.event_id WHERE EVENTS_user_xref.user_id > 0 AND EVENTS.placed_time > 0 GROUP BY EVENTS_user_xref.user_id UNION ALL
SELECT EVENTS_user_xref.user_id as user_id, MIN(EVENTS_archive.placed_time) as ts FROM EVENTS_archive JOIN EVENTS_user_xref ON EVENTS_archive.id = EVENTS_user_xref.event_id WHERE EVENTS_user_xref.user_id > 0 AND EVENTS_archive.placed_time > 0 GROUP BY EVENTS_user_xref.user_id UNION ALL
SELECT user_id, UNIX_TIMESTAMP(MIN(time)) as ts FROM user_action_log WHERE time > 0 AND user_id > 0 GROUP BY user_id UNION ALL
SELECT user_id, MIN(time) as ts FROM email_mask_log WHERE time > 0 AND user_id > 0 GROUP BY user_id
) uts
GROUP BY user_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment