+-------+----------------------+----------+------------------+
| isbn | book_container_id | shelf_id | update_time |
+-------+----------------------+----------+------------------+
| 555 | 6 | shelf100 | 11/15/2015 19:10 |
| 123 | 1 | shelf1 | 11/28/2015 8:00 |
| 555 | 4 | shelf5 | 11/28/2015 9:10 |
| 212 | 2 | shelf2 | 11/29/2015 8:10 |
| 555 | 6 | shelf9 | 11/30/2015 22:10 |
| 321 | 8 | shelf7 | 11/30/2015 8:10 |
| 555 | 4 | shelf33 | 12/1/2015 7:00 |
+-------+----------------------+----------+------------------+
Let's say I have a table (PostgreSQL) like the above called bookshelf_configuration
. If I'm given an ISBN and a timestamp, I want to be able to find the closest (before only) records for each unique combination of isbn
and book_container_id
.
So if I'm looking at isbn
'555', with a timestamp of '12/1/2015 7:00', I should get back:
+-------+----------------------+----------+------------------+
| isbn | book_container_id | shelf_id | update_time |
+-------+----------------------+----------+------------------+
| 555 | 6 | shelf9 | 11/30/2015 22:10 |
| 555 | 4 | shelf33 | 12/1/2015 7:00 |
+-------+----------------------+----------+------------------+
My knowledge of SQL is extremely basic. I've got a query that would work if I only had to factor in isbn, but I need some help understanding how to do this for the combination (isbn, book_container_id)
.