ROW_NUMBER()
window function to assign a rank to each activity for each user, ordered by startDate
in descending order.username
so that each user's activities are ranked separately.rnk
.rnk
is equal to 2, which represents the second most recent activity.UNION ALL
operator to ensure that both conditions are met.erDiagram UserActivity { varchar username varchar activity date startDate date endDate }