저는 Postgres 9.1을 사용하여 일부 데이터를 가져오고 있습니다.
나는 3 (가상/소독) 테이블이 있습니다여러 조건에서 개수를 사용하여 쿼리
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 |
을 그리고 created_at::date
과 student_id
별로 그룹화 다음과 같은 정보를 얻으려고 : 시험
- 총 수를 역사 에세이 (
exam.course = 'history'
) - 영어 수 (
exam.course = 'english'
)
이러한 쿼리 각각은 개별적으로 수행하기가 너무 어렵지는 않지만 함께 배치하는 것은 어렵습니다.
@David : 'GROUP BY 1'도 올바른 구문인지 알고 있습니까? [예.] (http://stackoverflow.com/questions/3800551/select-first-row-in-each-group-by-group/7630564#7630564) –
예, 저는 (지금) 그 사실을 알고 있습니다. 나는 그것을 조사해야만했으며, 질의가 더 명확하고, 더 부지런하며, 철자가 잘 잡히지 않는 것으로 나타났습니다. –