Files
homework/mdrozdov/postgresql/dm_course_summary.sql
T
2026-01-05 22:59:05 +07:00

17 lines
552 B
SQL

create materialized view dm_course_summary as
select
e.course_id,
c.title,
count(distinct e.user_id) as unique_users,
count(distinct l.id) as lessons_count,
count(distinct lv.id) as total_views,
round(count(distinct lv.id)*1.0/count(distinct e.user_id), 2) as avg_views_per_user,
min(lv.viewed_at) as first_viewed_at,
max(lv.viewed_at) as last_viewed_at
from enrollments e
join courses c on e.course_id = c.id
join lessons l on c.id = l.course_id
join lessons_views lv on lv.lesson_id = l.id
group by e.course_id, c.title
order by e.course_id