Skip to main content

window_functions

⚡ Implement Window Functions for Complex Analytics

Window functions like ROW_NUMBER(), RANK(), and LEAD() can be used via raw SQL fragments in ActiveRecord to perform advanced analytics without pulling data into Ruby. These queries let you rank, compute running totals, or compare rows within partitions.

users = User.select(
'users.*',
'RANK() OVER (PARTITION BY country ORDER BY score DESC) AS country_rank'
)

You can then filter or sort by country_rank in Ruby or SQL, which is perfect for multi-tenant leaderboards or trend analysis.