Last active
January 13, 2018 00:20
-
-
Save scwood/204882ae9d94818c0d5c7383dbaa18ab to your computer and use it in GitHub Desktop.
Join one to many relationship, select one row per the one on some criteria
This file contains hidden or 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
| -- one customer to many transactions | |
| -- want to select most recent transaction for each customer | |
| select * from customers join transactions on transactions.id = ( | |
| select id from transactions | |
| where transactions.customer_id = customers.id | |
| order by created_at desc | |
| limit 1 | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment