2014-02-18 3 views
0

저는 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::datestudent_id별로 그룹화 다음과 같은 정보를 얻으려고 : 시험

    • 총 수를 역사 에세이 (exam.course = 'history')
    • 영어 수 (exam.course = 'english')

    이러한 쿼리 각각은 개별적으로 수행하기가 너무 어렵지는 않지만 함께 배치하는 것은 어렵습니다.

  • 답변

    1
    SELECT created_at::date, student_id 
         ,count(*) AS exams 
         ,sum(CASE WHEN course = 'history' THEN essays ELSE 0 END) AS essays_hist 
         ,sum(CASE WHEN course = 'english' THEN essays ELSE 0 END) AS essays_engl 
    FROM public.exams x 
    LEFT JOIN (
        SELECT exam_id AS id, count(*) AS essays 
        FROM public.exams_essays 
        GROUP BY exam_id 
        ) s USING (id) 
    GROUP BY created_at::date, student_id; 
    

    는 I함으로써 시작하는 행의 승산을 회피 결합 전에 N 테이블 집계. 쿼리를 조금 더 간단하게 (IMO) 빠르게 수행합니다.

    +0

    @David : 'GROUP BY 1'도 올바른 구문인지 알고 있습니까? [예.] (http://stackoverflow.com/questions/3800551/select-first-row-in-each-group-by-group/7630564#7630564) –

    +0

    예, 저는 (지금) 그 사실을 알고 있습니다. 나는 그것을 조사해야만했으며, 질의가 더 명확하고, 더 부지런하며, 철자가 잘 잡히지 않는 것으로 나타났습니다. –

    1
    SELECT COUNT(DISTINCT EX.exam_id) TotalExams, 
         SUM(CASE WHEN E.course = 'history' THEN 1 ELSE 0 END) HistoryEssays, 
         SUM(CASE WHEN E.course = 'english' THEN 1 ELSE 0 END) EnglishEssays 
    FROM public.exams AS EX 
    LEFT JOIN public.exams_essays AS EE 
        ON EX.exam_id = EE.essay_id 
    
    +0

    하나의 시험에 여러 에세이가있을 수 있기 때문에 시험 카운트와 다를 수있는 에세이 카운트가 필요합니다. –

    +1

    @ DavidN.Welton 오, 알았어, 그걸 몰랐어. 지금 내 대답을 업데이트했습니다. – Lamak

    관련 문제