-
I have written this query here: groupedByDay:
SELECT v.id, v.startTime, v.endTime FROM DbTimeSpan v
JOIN (
(SELECT
date(endTime) AS date,
COUNT(*) AS timeSpanCount
FROM DbTimeSpan
GROUP BY date
) grouped
)
ON date(v.endTime) = grouped.date
ORDER BY date DESC, startTime DESC
LIMIT :limit OFFSET :offset; And I am not entirely sure why it doesn't work. What I want is a query that is paginated on the day boundary. If I increment the offset by one, it will return list of items that are part another day. Maybe this is bad practice and I should do the grouping in application code. Is this even possible with sqldelight? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
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
|
Beta Was this translation helpful? Give feedback.
-
You can test it with this dbfiddle (with data) https://www.db-fiddle.com/f/jLso8wgNRdQ8E8bS419W5Y/1
|
Beta Was this translation helpful? Give feedback.
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
This returns a list of objects from the query for the page