Compare commits

..

10 Commits

Author SHA1 Message Date
abogatyrev c0f5f0dc5e Merge pull request 'add homework2' (#114) from test1232131231232131231 into main
Reviewed-on: https://gitea.de-infra.servehttp.com/de-infrastructure/DWH/pulls/114
2026-03-02 11:27:06 +04:00
Aleksey Bogatyrev 868ea8ca03 fix homework2.sql 2026-03-02 11:26:12 +04:00
Aleksey Bogatyrev e2029b1f74 add homework2 2026-03-02 11:24:16 +04:00
Aleksey Bogatyrev 7d0193e046 add postgresql homework.sql 2026-03-02 11:21:28 +04:00
abogatyrev 13d505ea1c Merge pull request 'SQL Homework' (#52) from sql_homework into main
Reviewed-on: https://gitea.de-infra.servehttp.com/de-infrastructure/DWH/pulls/52
Reviewed-by: aleksey bogatyrev <abogatyrev@example.com>
2026-03-01 13:06:59 +04:00
Alex cd4e5335aa PET task 4 finished 2026-02-28 16:07:09 +03:00
Alex de4a0fa2ec PET task 4 fixed 2026-02-27 15:31:56 +03:00
Alex 5854cdbd21 Name changed to task3 2026-02-26 15:21:31 +03:00
Alex ace9b8b292 Task 2 fixed 2026-02-26 15:17:39 +03:00
Alex c95a867e13 PET Extra Tasks completed 2026-02-24 14:58:16 +03:00
5 changed files with 82 additions and 5 deletions
+26
View File
@@ -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
;
+28
View File
@@ -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
;
+26 -5
View File
@@ -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% пользователей были зарегистрированы в эти месяцы.
+1
View File
@@ -0,0 +1 @@
select * from users;
+1
View File
@@ -0,0 +1 @@
select * from users;