advanced
Complex Time-Series Analysis with SQL
Solve a retention analysis problem using advanced window functions.
📝 Contenido del prompt
Write a SQL query to calculate the cohort retention rate for a subscription service. The input is a table of user logs (user_id, log_date, action). Your query must calculate the percentage of users who return in the 1st, 2nd, and 3rd months after their initial signup. Use window functions such as LAG, LEAD, and FIRST_VALUE to achieve this without using self-joins. Handle the scenario where a user churns and returns, ensuring they are counted based on their latest cohort.