Created
June 22, 2016 09:25
-
-
Save inscapist/ac51d4423f553135cd265e0ca99b7bf2 to your computer and use it in GitHub Desktop.
SQL example for self-join
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
prev.investment_id, | |
prev.from as prev_from, | |
prev.to as prev_to, | |
next.from as next_from, | |
next.to as next_to, | |
to_date(cast(prev.created_at as TEXT), 'YYYY-MM-DD') as prev_created_at, | |
to_date(cast(next.created_at as TEXT), 'YYYY-MM-DD') as next_created_at | |
FROM investment_state_transitions as prev | |
INNER JOIN investment_state_transitions as next | |
ON | |
prev.investment_id = next.investment_id | |
AND | |
prev.to = next.from | |
WHERE | |
prev.to = 'confirmed_bid' | |
AND | |
date(prev.created_at) <= '2016-05-22' | |
AND | |
date(next.created_at) > '2016-05-22' | |
ORDER BY prev.investment_id ASC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment