+-------+----------------------+----------+------------------+
| 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 |
+-------+----------------------+----------+------------------+
假设我有一个与上面类似的表 (PostgreSQL),名为 bookshelf_configuration
.如果给我一个 ISBN 和一个时间戳,我希望能够为 isbn
的每个唯一组合找到最接近的(仅之前)记录和 book_container_id
.
所以如果我正在查看 isbn
“555”,时间戳为“12/1/2015 7:00”,我应该回来:
+-------+----------------------+----------+------------------+
| isbn | book_container_id | shelf_id | update_time |
+-------+----------------------+----------+------------------+
| 555 | 6 | shelf9 | 11/30/2015 22:10 |
| 555 | 4 | shelf33 | 12/1/2015 7:00 |
+-------+----------------------+----------+------------------+
我对 SQL 的了解非常基础。我有一个查询,如果我只需要考虑 isbn 就可以工作,但我需要一些帮助来了解如何为组合 (isbn, book_container_id)
执行此操作.
最佳答案
DISTINCT ON
的典型用例:
SELECT DISTINCT ON (book_container_id)
isbn, book_container_id, shelf_id, update_time
FROM bookshelf_configuration
WHERE isbn = 555
AND update_time <= '2015-12-01 07:00' -- ISO 8601 format
ORDER BY book_container_id, update_time DESC;
假设 update_time
定义为 NOT NULL
,或者您必须添加 NULLS LAST
。详细解释:
根据基数和值频率,可能会有更快的查询样式:
无论哪种方式,multicolumn index在 (isbn, book_container_id, update_time DESC)
上是使非平凡大小的表快速运行的关键。排序顺序应与查询匹配(或者是完全反转)。如果将 NULLS LAST
添加到查询中,请将其也添加到索引中。
旁白:最好对所有日期/时间常量使用 ISO 8601 格式,因为这对于任何语言环境或日期样式设置都是明确的。相关:
关于sql - 查询以获取具有最接近时间戳值的记录以获取两列的唯一组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34052157/