I'm using Postgres 9.1 to try and fetch some data.
I have three (fictitious/sanitized) tables:
Table "public.students"
Column | Type | Modifiers
--------------+-----------------------------+-----------
id | uuid | not null
name | character varying |
birth_date | date |
last_exam_at | timestamp without time zone |
created_at | timestamp without time zone |
code | character varying |
gender | character varying |
Indexes:
"students_id_key" UNIQUE CONSTRAINT, btree (id)
Referenced by:
TABLE "exams" CONSTRAINT "exams_student_id_fkey"
FOREIGN KEY (student_id) REFERENCES students(id)
Table "public.exams_essays"
Column | Type | Modifiers
----------+------+-----------
exam_id | uuid |
essay_id | uuid |
Table "public.exams"
Column | Type | Modifiers
-------------------+-----------------------------+-----------
id | uuid | not null
student_id | uuid |
created_at | timestamp without time zone |
completed_at | timestamp without time zone |
course | character varying |
Table "public.essays"
Column | Type | Modifiers
-----------------+-----------------------------+-----------
id | uuid | not null
essay_type | character varying | not null
filename | character varying |
And I'm trying to get the following information, grouped by created_at::date
and student_id
:
- total number of exams
- number of history essays (
exam.course = 'history'
) - number of english essays (
exam.course = 'english'
)
Each of these queries are not too difficult to do individually, but putting them together is proving difficult.