I have a table that I'm first trying to group based on unique column values (using dense_rank) and then further group those items into batches of 5. Below is my table:
| video_id | frame_id | verb |
|---|---|---|
| video_a | frame_1 | walk |
| video_a | frame_2 | run |
| video_a | frame_3 | sit |
| video_a | frame_4 | walk |
| video_a | frame_5 | walk |
| video_a | frame_6 | walk |
| video_b | frame_7 | stand |
| video_b | frame_8 | stand |
| video_b | frame_9 | run |
| video_b | frame_10 | run |
| video_b | frame_11 | sit |
| video_b | frame_12 | run |
| video_b | frame_13 | run |
And below is what I'm trying to get:
| video_id | frame_id | verb | batch_of_five |
|---|---|---|---|
| video_a | frame_1 | walk | 1 |
| video_a | frame_2 | run | 1 |
| video_a | frame_3 | sit | 1 |
| video_a | frame_4 | walk | 1 |
| video_a | frame_5 | walk | 1 |
| video_a | frame_6 | walk | 2 |
| video_b | frame_7 | stand | 3 |
| video_b | frame_8 | stand | 3 |
| video_b | frame_9 | run | 3 |
| video_b | frame_10 | run | 3 |
| video_b | frame_11 | sit | 3 |
| video_b | frame_12 | run | 4 |
| video_b | frame_13 | run | 4 |
Where each video_id has a unique rank and each batch of 10 within each ranked video_id has its own unique rank (and each batch of 10 overall has a unique id regardless of whether they belong to the same video_id or not).
I'm able to group based on the video_id column but am having trouble grouping those items further so that they are both in batches of 10 and unique across all video_ids. I thought about using a group by clause but I'm trying to keep the other columns intact as well (verb column).
Here is my presto query so far:
SELECT
*
FROM (
SELECT
*,
-- Give each unique video_id a unique rank
DENSE_RANK() OVER (ORDER BY video_id) AS video_batch
FROM videos
)