Thanks to visit codestin.com
Credit goes to github.com

Skip to content

How do write paginated query that groups by date and returns list of items? (code doens't work) #5504

Answered by griffio
mainrs asked this question in Q&A
Discussion options

You must be logged in to vote

I don't think that LIMIT/OFFSET approach will work for your groupByDay as it works on rows not "pages"

You could try (assuming Sqlite) windows function - currently in snapshot version for Sqlite #2799

e.g Creates pages 1...n using dense_rank over the date part of the timestamp and :page_num is the parameter to set for desired page

groupedByDay:
WITH daily_groups AS (
  SELECT
    id, startTime, endTime,
    DATE(startTime, 'unixepoch') AS date,
    DENSE_RANK() OVER (ORDER BY DATE(startTime, 'unixepoch')) AS day_num
  FROM DbTimeSpan
)
SELECT *
FROM daily_groups
WHERE day_num = :page_num; // page 1 to n

This returns a list of objects from the query for the page

GroupedByDay(id=1, startTim…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@mainrs
Comment options

Answer selected by mainrs
Comment options

You must be logged in to vote
1 reply
@mainrs
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants