Using Window Functions To Get Top N per Group
Last updated
Was this helpful?
Last updated
Was this helpful?
Given the schema presented below write a query, which uses a window function, that returns two most viewed posts for every category.
Order the result set by:
category name alphabetically
number of post views largest to lowest
post id lowest to largest
Note:
Some categories may have less than two or no posts at all.
Two or more posts within the category can be tied by (have the same) the number of views. Use post id as a tie breaker - a post with a lower id gets a higher rank.
The desired output should look like this:
category_id
- category id
category
- category name
title
- post title
views
- the number of post views
post_id
- post id