It seems you need in something similar to:
WITH cte AS ( SELECT *,
RANK() OVER (PARTITION BY clients.id
ORDER BY stages.position DESC) rnk
INNER JOIN deals ON deals.client_id = clients.id
INNER JOIN deal_stages ON deal_stages.deal_id = deals.id
In my environment, we replicate a lot of tables from production server to the reports server. On reports server we have plenty of views that are based on replicated production tables AND are nested. Before replication starts, we have to remove all views to make replication possible (we use drop and create because tables structure often change in production). ...