From 88db25d7d559ffc6b121bcf5782a29429f966226 Mon Sep 17 00:00:00 2001 From: dr0zd Date: Tue, 6 Jan 2026 16:36:58 +0700 Subject: [PATCH] added safety div with nullif --- mdrozdov/postgresql/dm_course_summary.sql | 2 +- mdrozdov/postgresql/dm_platform_summary.sql | 4 ++-- mdrozdov/postgresql/dm_user_activity_summary.sql | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mdrozdov/postgresql/dm_course_summary.sql b/mdrozdov/postgresql/dm_course_summary.sql index 434ffe3..40b4b8d 100644 --- a/mdrozdov/postgresql/dm_course_summary.sql +++ b/mdrozdov/postgresql/dm_course_summary.sql @@ -5,7 +5,7 @@ select 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, + round(count(distinct lv.id)*1.0/nullif(count(distinct e.user_id), 0), 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 diff --git a/mdrozdov/postgresql/dm_platform_summary.sql b/mdrozdov/postgresql/dm_platform_summary.sql index 7be6f5c..146fde7 100644 --- a/mdrozdov/postgresql/dm_platform_summary.sql +++ b/mdrozdov/postgresql/dm_platform_summary.sql @@ -4,5 +4,5 @@ select (select count(id) from courses) as total_courses, (select count(id) from lessons) as total_lessons, (select count(distinct user_id) from lessons_views) as users_with_views, - round((select count(id) from lessons)*1.0/(select count(id) from courses), 2) as avg_lessons_per_course, - (select count(id) from lessons_views)*1.0/(select count(id) from lessons) \ No newline at end of file + round((select count(id) from lessons)*1.0/nullif((select count(id) from courses), 0), 2) as avg_lessons_per_course, + round((select count(id) from lessons_views)*1.0/nullif((select count(id) from lessons), 0), 2) as avg_views_per_lesson \ No newline at end of file diff --git a/mdrozdov/postgresql/dm_user_activity_summary.sql b/mdrozdov/postgresql/dm_user_activity_summary.sql index f2539b6..413d937 100644 --- a/mdrozdov/postgresql/dm_user_activity_summary.sql +++ b/mdrozdov/postgresql/dm_user_activity_summary.sql @@ -19,7 +19,7 @@ with enrollment_counts as( group by user_id order by user_id ), engagment_rate as ( - select tl.user_id, round(v.viewed_lessons_count*100.0/total_lessons, 2) as engagment_rate + select tl.user_id, round(v.viewed_lessons_count*100.0/nullif(total_lessons), 2) as engagment_rate from total_lessons tl join views v using(user_id) )