最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sql - Calculate weekly retention each day - Stack Overflow

programmeradmin4浏览0评论

I'm looking to calculate NURR, CURR and RURR each day for some user data from an app. I've started with NURR (New User Retention Rate), which is defined as:

The percentage of users who first opened the app between t-13 and t-7, that open the app between t-6 and t-0

For each user I have a unique ID, the date of each session in the app, and the date of their first session.

| user_id | session_date | first_session |
|---------|--------------|---------------|
|    1    |  2025-02-17  |  2025-02-17   |
|    1    |  2025-02-18  |  2025-02-18   |
|    2    |  2025-02-17  |  2025-01-31   |
|    2    |  2025-02-18  |  2025-02-18   |

I know how to calculate NURR for a specific day, e.g., today.

SELECT SAFE_DIVIDE(COUNT(DISTINCT this_week.user_id), COUNT(DISTINCT last_week.user_id))
FROM
(SELECT DISTINCT user_id
FROM sessions
WHERE session_date <= current_date() - 7
AND session_date >= current_date() - 13
AND first_session <= current_date() - 7
AND first_session >= current_date() - 13) last_week
LEFT JOIN
(SELECT DISTINCT user_id
FROM sessions
WHERE session_date <= current_date()
AND session_date >= current_date() - 6) this_week
ON last_week.user_id = this_week.user_id

However, I don't know how to build on this to calculate NURR for different days, e.g.,

|    date    | NURR |
|------------|------|
| 2025-02-18 | 0.35 |
| 2025-02-17 | 0.34 |
| 2025-02-16 | 0.33 |
| 2025-02-15 | 0.34 |
| 2025-02-14 | 0.35 |

From the research I've done, it sounds like the answer might involve OVER and partitions, but all of the examples I can find use very simple aggregate functions, so I can't figure out how to apply it to my case.

发布评论

评论列表(0)

  1. 暂无评论