DatabaseCreation-AdvDb: views.sql

File views.sql, 5.3 KB (added by 231175, 2 weeks ago)
Line 
1-- view #1 - get enrolled courses for a user (for a specific user its one where clause in the end)
2drop view if exists enrolled_courses;
3create or replace view enrolled_courses as
4(
5select
6 e.user_id as user_id,
7 cv.version_number as course_version_id,
8 cv.active as version_is_active,
9 cv.creation_date as version_creation_date,
10 c.id as course_id,
11 c.image_url,
12 c.color,
13 c.difficulty,
14 c.duration_minutes,
15 ct.title_short,
16
17 (select array_agg(st.name order by st.name)
18 from skill s
19 join course_skill cs on cs.skill_id = s.id
20 join skill_translation st on st.skill_id = s.id
21 join language l on l.id = st.language_id
22 where cs.course_id = c.id
23 and l.language_code = 'EN') as skills_learned,
24
25 (select array_agg(tt.name order by tt.name)
26 from topic t
27 join course_topic ct on ct.topic_id = t.id
28 join topic_translation tt on tt.topic_id = t.id
29 join language l on l.id = tt.language_id
30 where ct.course_id = c.id
31 and l.language_code = 'EN') as topics_covered
32
33from course_version cv
34 join enrollment e on e.course_version_id = cv.id
35 join course c on c.id = cv.course_id
36 join course_translation ct on ct.course_id = c.id
37 join language l on l.id = ct.language_id
38where l.language_code = 'EN'
39 );
40
41select *
42from enrolled_courses;
43
44
45-- view #2 - get detailed information about an enrolled course for a user (for a specific user its one where clause in the end)
46drop view if exists enrolled_course_details;
47create or replace view enrolled_course_details as
48select
49 e.user_id,
50 cv.version_number as course_version_id,
51 cv.active as version_is_active,
52 cv.creation_date as version_creation_date,
53 c.id as course_id,
54 c.image_url,
55 c.color,
56 c.difficulty,
57 c.duration_minutes,
58 ct.title,
59 ct.description,
60 ct.description_long,
61
62 (
63 select json_agg(
64 json_build_object(
65 'module_id', cm.id,
66 'position', cm.position,
67 'title', cmt.title,
68
69 'lectures',
70 (
71 select json_agg(
72 json_build_object(
73 'lecture_id', cl.id,
74 'position', cl.position,
75 'duration_minutes', cl.duration_minutes,
76 'content_type', cl.content_type,
77 'title', clt.title,
78 'description', clt.description,
79 'content_file_name', clt.content_file_name,
80 'content_text', clt.content_text
81 )
82 order by cl.position
83 )
84 from course_lecture cl
85 join course_lecture_translation clt
86 on clt.course_lecture_id = cl.id
87 join language l2
88 on l2.id = clt.language_id
89 where cl.course_module_id = cm.id
90 and l2.language_code = 'EN'
91 )
92 )
93 order by cm.position
94 )
95 from course_module cm
96 join course_module_translation cmt
97 on cmt.course_module_id = cm.id
98 join language l1
99 on l1.id = cmt.language_id
100 where cm.course_version_id = cv.id
101 and l1.language_code = 'EN'
102 ) as modules
103
104from course_version cv
105 join enrollment e
106 on e.course_version_id = cv.id
107 join course c
108 on c.id = cv.course_id
109 join course_translation ct
110 on ct.course_id = c.id
111 join language l
112 on l.id = ct.language_id
113where l.language_code = 'EN';
114
115select * from enrolled_course_details;
116
117
118-- view #3 - top 10 best-selling courses, platform wide
119drop view if exists best_selling_courses;
120create or replace view best_selling_courses as
121select
122 c.id as course_id,
123 c.difficulty,
124 c.duration_minutes,
125 ct.title_short,
126
127 count(e.user_id) as total_enrollments
128
129from course c
130 join course_version cv on cv.course_id = c.id
131 join enrollment e on e.course_version_id = cv.id
132 join course_translation ct on ct.course_id = c.id
133 join language l on l.id = ct.language_id
134where l.language_code = 'EN'
135group by c.id, ct.id
136order by total_enrollments desc
137limit 10;
138
139select * from best_selling_courses;