PET 2nd task fixed

This commit is contained in:
Alex
2026-02-23 15:06:03 +03:00
parent 4d56aa1f5b
commit 9dedbfc3eb
+6 -13
View File
@@ -237,7 +237,6 @@ left join possible_lessons pl on vc.user_id = pl.user_id
------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------
-- 2 задание Эффективность курсов -- 2 задание Эффективность курсов
create materialized view course_summary AS create materialized view course_summary AS
with lessons_per_course as ( with lessons_per_course as (
select c.id as course_id, c.title as course_title, select c.id as course_id, c.title as course_title,
@@ -249,26 +248,20 @@ create materialized view course_summary AS
group by c.id, c.title group by c.id, c.title
), ),
course_views as ( course_views as (
select l.course_id, COUNT(lv.id) as total_views select l.course_id,
from lesson_views lv COUNT(lv.id) as total_views,
join lessons l on lv.lesson_id = l.id
group by l.course_id
),
view_date as (
select l.course_id,
MIN(lv.viewed_at) as first_viewed_at, MIN(lv.viewed_at) as first_viewed_at,
MAX(lv.viewed_at) as last_viewed_at MAX(lv.viewed_at) as last_viewed_at
from lessons l from lesson_views lv
join lesson_views lv on l.id = lv.lesson_id join lessons l on lv.lesson_id = l.id
group by l.course_id group by l.course_id
) )
select lpc.course_id, lpc.course_title, lpc.unique_users, lpc.lessons_count, select lpc.course_id, lpc.course_title, lpc.unique_users, lpc.lessons_count,
ROUND(1.00 * cv.total_views / lpc.unique_users, 2) as avg_views_per_user, ROUND(1.00 * cv.total_views / lpc.unique_users, 2) as avg_views_per_user,
vd.first_viewed_at, cv.first_viewed_at,
vd.last_viewed_at cv.last_viewed_at
from lessons_per_course lpc from lessons_per_course lpc
join course_views cv on lpc.course_id = cv.course_id join course_views cv on lpc.course_id = cv.course_id
left join view_date vd on cv.course_id = vd.course_id
order by course_id order by course_id
; ;