Fetching the top n per group with window functions

Have you ever needed to get the most recent N posts for each user in rails, but didn’t know how to do it without using map?Or maybe something similar like:The first or last X comments for each postThe first or last Y payments for each customerThe first or last Z reviews for each customerSometimes could be ok to just fetch all elements and filter with ruby, but sometimes it is not possible. Also it can cause n 1 queries if your are not careful.Here I want to show you how you can solve this problem using window functions.
Fetching the top n per group with window functions #ruby #rubydeveloper #rubyonrails https://rubyonrails.ba/single/fetching-the-top-n-per-group-with-window-functions

Nezir Zahirovic

Contractor Ruby On Rails (8+ years) / MCPD .Net / C# / Asp.Net / CSS / SQL / (11 years)

related articles