Compare commits
10 Commits
9dedbfc3eb
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c0f5f0dc5e | |||
| 868ea8ca03 | |||
| e2029b1f74 | |||
| 7d0193e046 | |||
| 13d505ea1c | |||
| cd4e5335aa | |||
| de4a0fa2ec | |||
| 5854cdbd21 | |||
| ace9b8b292 | |||
| c95a867e13 |
@@ -0,0 +1,26 @@
|
|||||||
|
CREATE MATERIALIZED VIEW platform_summary AS
|
||||||
|
with users_counts as (
|
||||||
|
select
|
||||||
|
COUNT(distinct u.id) as total_users,
|
||||||
|
COUNT(distinct lv.user_id) as users_with_views
|
||||||
|
from users u
|
||||||
|
left join lesson_views lv on lv.user_id = u.id
|
||||||
|
),
|
||||||
|
courses_lessons_count as (
|
||||||
|
select COUNT(distinct c.id) as total_courses,
|
||||||
|
COUNT(distinct l.id) as total_lessons
|
||||||
|
from courses c
|
||||||
|
left join lessons l on l.course_id = c.id
|
||||||
|
),
|
||||||
|
views_count as (
|
||||||
|
select COUNT(distinct lv.lesson_id) as lessons_with_views,
|
||||||
|
COUNT(distinct lv.id) as total_views
|
||||||
|
from lesson_views lv
|
||||||
|
)
|
||||||
|
select uc.total_users, clc.total_courses, clc.total_lessons, uc.users_with_views,
|
||||||
|
ROUND(1.00 * clc.total_lessons / nullif(clc.total_courses, 0), 2) as avg_lessons_per_course,
|
||||||
|
ROUND(1.00 * vc.total_views / nullif(clc.total_lessons, 0), 2) as avg_views_per_lesson
|
||||||
|
from users_counts uc
|
||||||
|
cross join courses_lessons_count clc
|
||||||
|
cross join views_count vc
|
||||||
|
;
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
CREATE MATERIALIZED VIEW users_cohort_analysis AS
|
||||||
|
with needed_users as (
|
||||||
|
select distinct u.id as user_id,
|
||||||
|
u.registration_date as reg_date, e.course_id, lv.id as view_id,
|
||||||
|
lv.lesson_id as viewed_lesson_id,
|
||||||
|
FIRST_VALUE(lv.viewed_at) over (partition by lv.user_id order by lv.viewed_at) as first_view_date
|
||||||
|
from users u
|
||||||
|
left join enrollments e on u.id = e.user_id
|
||||||
|
left join lesson_views lv on e.user_id = lv.user_id
|
||||||
|
order by user_id
|
||||||
|
),
|
||||||
|
CTE as (
|
||||||
|
select
|
||||||
|
TO_CHAR(reg_date, 'YYYY-MM') as cohort_month,
|
||||||
|
COUNT(distinct user_id) as users_in_cohort,
|
||||||
|
COUNT(distinct case when user_id in (select user_id from enrollments) then user_id end) as enrolled_users,
|
||||||
|
COUNT(distinct case when user_id in (select user_id from lesson_views) then user_id end) as active_users,
|
||||||
|
COUNT(course_id) as courses_count,
|
||||||
|
extract('days' from AVG(first_view_date - reg_date)) as avg_time_to_first_view
|
||||||
|
from needed_users
|
||||||
|
group by cohort_month
|
||||||
|
)
|
||||||
|
select cohort_month, users_in_cohort, enrolled_users, active_users,
|
||||||
|
ROUND(100.0 * active_users / users_in_cohort, 2) as retention_rate,
|
||||||
|
courses_count::numeric / users_in_cohort as avg_courses_per_user,
|
||||||
|
avg_time_to_first_view
|
||||||
|
from CTE
|
||||||
|
;
|
||||||
@@ -283,7 +283,7 @@ from total_counts
|
|||||||
;
|
;
|
||||||
|
|
||||||
-----------------------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------------------
|
||||||
-- 4 Задание Когортный анализ пользователей
|
-- 4 Задание Когортный анализ пользователей (Для каждого месяца регистрации)
|
||||||
create materialized view user_cohort_analysis AS
|
create materialized view user_cohort_analysis AS
|
||||||
with needed_users as (
|
with needed_users as (
|
||||||
select distinct u.id as user_id, u.registration_date as reg_date, e.course_id, lv.id as view_id,
|
select distinct u.id as user_id, u.registration_date as reg_date, e.course_id, lv.id as view_id,
|
||||||
@@ -305,7 +305,6 @@ CTE as (
|
|||||||
COUNT(course_id) as courses_count,
|
COUNT(course_id) as courses_count,
|
||||||
extract('days' from AVG(first_view_date - reg_date)) as avg_time_to_first_view
|
extract('days' from AVG(first_view_date - reg_date)) as avg_time_to_first_view
|
||||||
from needed_users
|
from needed_users
|
||||||
where date_trunc('month', reg_date)::date = '2023-03-01'
|
|
||||||
group by cohort_month
|
group by cohort_month
|
||||||
)
|
)
|
||||||
select cohort_month, users_in_cohort, enrolled_users, active_users,
|
select cohort_month, users_in_cohort, enrolled_users, active_users,
|
||||||
@@ -340,7 +339,7 @@ views_cte as (
|
|||||||
select lic.lesson_id, lic.lesson_title, lic.course_title,
|
select lic.lesson_id, lic.lesson_title, lic.course_title,
|
||||||
vc.total_views,
|
vc.total_views,
|
||||||
vc.unique_viewers,
|
vc.unique_viewers,
|
||||||
coalesce(vc.avg_views_per_user, 0),
|
coalesce(vc.avg_views_per_user, 0) as avg_views_per_user,
|
||||||
dense_rank() OVER (ORDER BY vc.total_views desc) as popularity_rank,
|
dense_rank() OVER (ORDER BY vc.total_views desc) as popularity_rank,
|
||||||
ROUND(100.0 * unique_viewers / enrolled_to_course, 2) as completion_rate
|
ROUND(100.0 * unique_viewers / enrolled_to_course, 2) as completion_rate
|
||||||
from lessons_in_courses lic
|
from lessons_in_courses lic
|
||||||
@@ -441,5 +440,27 @@ funnel_stages as (
|
|||||||
;
|
;
|
||||||
-- Признаюсь 6-е задание сделал с помощью нейросети, потому что сам вообще никак к нему подобраться не мог.
|
-- Признаюсь 6-е задание сделал с помощью нейросети, потому что сам вообще никак к нему подобраться не мог.
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------------------------------------------
|
||||||
|
-- Доп. Задание №1 Анализ drop-off точек
|
||||||
|
select course_id, stage_name, users_count,
|
||||||
|
100 - conversion_from_previous as lost_precentage
|
||||||
|
from conversion_funnel
|
||||||
|
where stage_name = 'started'
|
||||||
|
order by lost_precentage desc
|
||||||
|
;
|
||||||
|
--------------------------------------------------------------------------------------------------------------------
|
||||||
|
-- Доп. Задание №2 Топ-3 урока с повторными просмотрами
|
||||||
|
select lesson_id
|
||||||
|
from lesson_popularity_ranking
|
||||||
|
where avg_views_per_user > 1
|
||||||
|
limit 3
|
||||||
|
;
|
||||||
|
---------------------------------------------------------------------------------------------------------------------
|
||||||
|
-- Доп. Задание №3 Лучшая когорта по retention
|
||||||
|
select cohort_month, retention_rate
|
||||||
|
from user_cohort_analysis
|
||||||
|
order by retention_rate desc
|
||||||
|
limit 2
|
||||||
|
;
|
||||||
|
-- В нашем случае два месяца с одинаковыми лучшими показателями retention это 2023-01 и 2023-02. Это является следствием того, что у нас
|
||||||
|
-- наибольшее количество зарегистрированных пользователей именно в эти месяцы, а именно 2023-01 = 40%, а 2023-02 = 20%, соотвественно 60% пользователей были зарегистрированы в эти месяцы.
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
select * from users;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
select * from users;
|
||||||
Reference in New Issue
Block a user