Please see this post for an article explaining the use of PARTITION BY in the window functions used in this query.