Skip to content

Instantly share code, notes, and snippets.

@dcorking
Forked from alicethewhale/code.rb
Last active November 22, 2018 16:36
Show Gist options
  • Save dcorking/be82ca1fa9f1ae447c6321054a2edb48 to your computer and use it in GitHub Desktop.
Save dcorking/be82ca1fa9f1ae447c6321054a2edb48 to your computer and use it in GitHub Desktop.
SELECT timeline_items.* FROM (
# Query 1
SELECT lead_alerts.id AS id, lead_alerts.action AS body, users.first_name AS first_name, users.last_name AS last_name, lead_alerts.due_on AS date, "lead_alerts" AS model
FROM `lead_alerts` LEFT OUTER JOIN `users` ON `users`.`id` = `lead_alerts`.`author_id`
WHERE `lead_alerts`.`lead_id` = 1
UNION ALL
# Query 2
SELECT completed_lead_alerts.id AS id, lead_alerts.action AS body, users.first_name AS first_name, users.last_name AS last_name, completed_lead_alerts.completed_on AS date, "completed_lead_alerts" AS model
FROM `completed_lead_alerts`
INNER JOIN `lead_alerts` ON `lead_alerts`.`id` = `completed_lead_alerts`.`lead_alert_id`
INNER JOIN `users` ON `users`.`id` = `completed_lead_alerts`.`completor_id`
WHERE `lead_alerts`.`lead_id` = 1
UNION ALL
# Query 3
SELECT notes.id AS id, notes.body AS body, users.first_name AS first_name, users.last_name AS last_name, notes.date AS date, "notes" AS model FROM `notes`
INNER JOIN `users` ON `users`.`id` = `notes`.`author_id`
WHERE `notes`.`lead_id` = 1
UNION ALL
# Query 4
SELECT lead_transitions.id AS id, lead_transitions.to_state AS body, "placeholder" AS first_name, "placeholder" AS last_name, lead_transitions.created_at AS date, "lead_transitions" AS model
FROM `lead_transitions`
WHERE `lead_transitions`.`lead_id` = 1
) AS timeline_items
WHERE (timeline_items.date <= '2018-11-11 10:07:33.672915')
ORDER BY timeline_items.date DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment