Source
ORDER BY
Specifies an ordering of the rows of the result table from a SELECT list.
A query using a correlated column inside a window function fails with internal error 300010:391167117
SOLUTION: To resolve this issue, avoid using correlated columns inside a window function. This can be achieved by moving the window aggregation function to the outer query.