문제

Say I have a table my_table that has a column ID and has another column Date and all these records are unique.

I would like to select for all IDs, the records with the lowest N Dates. If I want to do this for N = 1, I have the following below

SELECT
    *
FROM my_table r
WHERE
    r.Date = (SELECT Min(Date) FROM my_table WHERE ID = r.ID)

How do I generalise this for higher N?

도움이 되었습니까?

해결책

SQLite allows the following syntax:

SELECT r.*
FROM my_table r
WHERE r.Date IN (SELECT Date FROM my_table WHERE ID = r.ID ORDER BY Date LIMIT 5);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top