Tag Archives: max

Select top 2 rows from records, grouped/partitioned by Unique ID

The Problem: A client needed a query that selected the two most recent tour dates for each user ID. This is an easy task for a single user ID because specifying the user ID and an ORDER BY tour_date DESC with a LIMIT 2 clause would get the dates needed. However, when there are a lot of users it gets harder. For a single date, the MAX function works great but in our instance it will not work because we