I’m working on a database query for training sessions and need help with pagination.
My table returns data like this:
training_id | session_date | student_name
-----------+--------------+-------------
TR001 | 2022-01-15 | Mike Smith
TR001 | 2022-01-15 | Sarah Jones
TR001 | 2022-01-16 | Mike Smith
TR001 | 2022-01-16 | Sarah Jones
Each training course appears multiple times because of different dates and students. I need to paginate by getting the first 25 unique training courses, not 25 total rows.
My current query doesn’t work properly:
SELECT training_id, session_date, student_name
FROM training_sessions
ORDER BY session_date
OFFSET 0
FETCH NEXT 25 ROWS ONLY
This gives me 25 rows but they might represent only a few actual training courses since each course has multiple entries.
I tried using a CTE approach:
WITH unique_trainings AS
(
SELECT training_id
FROM training_sessions
WHERE session_date <= '2022-12-31'
ORDER BY training_id
OFFSET 0 ROWS
FETCH NEXT 25 ROWS ONLY
)
SELECT *
FROM training_sessions t
INNER JOIN unique_trainings ut ON ut.training_id = t.training_id
WHERE session_date <= '2022-12-31'
Is there a better way to limit results based on distinct column values rather than total row count?