我在PostgreSQL中有如下查询:
select count(id_student) students, date_beginning_course from
data.sessions_courses
left join my_schema.students on id_session_course=id_sesion
where course_name='First course'
group by date_beginning_course
我通过此查询得到的是参加了多个日期“第一课程”会话的学生人数,例如:
Students Date_beginning_course
____________________________________
5 2019-06-26
1 2019-06-28
5 2019-06-30
6 2019-07-01
2 2019-07-02
我希望填充这个表格中缺失的日期值,并且对于每个缺失的值,在“学生”列中分配一个“0”,因为该日期没有学生。 例如:
Students Date_beginning_course
____________________________________
5 2019-06-26
0 2019-06-27 <--new row
1 2019-06-28
0 2019-06-29 <--new row
5 2019-06-30
6 2019-07-01
2 2019-07-02
你能帮我吗?谢谢!:)
日历表
(可能使用generate-series()
函数)进行左连接。 - wildplassergenerate_series()
创建日期范围,如@wildplassers 答案所述。 - Bohemian