2011-10-14 3 views
1

용어마다 테스트 횟수를 반환하려고합니다. 카운트가 돌아 오도록 할 수는 있지만 용어로 그룹화 할 수는 없습니다.쿼리 광의로 구분되는 count, group

나는 모든 것을 시도하고 내가 할 가장 가까운는 옳지 않아 내 계산만을 = 1, 용어에 의해 그룹화에 불과하다.

여기에 제가 지금 있습니다. 그냥 count를 반환합니다. 어떻게 term_id로 그룹화합니까? 사용자의 설정에 대한 자세한 내용을 모른 채

SELECT COUNT(*) 
    FROM (SELECT DISTINCT ON(student_id, test_event_id, terf.term_id) student_id 
      FROM report.test_event_result_fact terf 
      JOIN report.growth_measurement_window gw on gw.term_id = terf.term_id 
      JOIN report.term t on t.term_id = terf.term_id 
      JOIN report.test tt on tt.test_id = terf.test_id 
     WHERE terf.partner_id = 98 
      AND growth_event_yn = 't' 
      AND gw.test_window_complete_yn = 't' 
      AND gw.growth_window_type = 'DISTRICT' 
      AND tt.test_type_description = 'SURVEY_WITH_GOALS') as TestEvents 
+3

조각 - 쉽게 읽을의 가능성은 학기당 –

+0

테스트 도움을 얻으려면? 또는 한 학기당 학생 당 테스트? –

+1

힌트 : 관련없는 모든 조인을 없애고 질문/문제를 * 가능한 가장 단순한 형식으로 제시하면 사람들이 당신을 도울 가능성이 커집니다 * – Bohemian

답변

1

, 그게 내 최선의 방법입니다 : 조언

select term_id, count(*) AS count_per_term 
    from (
    select Distinct on (student_id, test_event_id, terf.term_id) 
      terf.term_id, student_id 
     from report.test_event_result_fact terf 
     join report.growth_measurement_window gw using (term_id) 
     join report.term t using (term_id) 
     join report.test tt using (term_id) 
    where terf.partner_id = 98 
     and growth_event_yn = 't' 
     and gw.test_window_complete_yn = 't' 
     and gw.growth_window_type = 'DISTRICT' 
     and tt.test_type_description = 'SURVEY_WITH_GOALS') as TestEvents 
    group by 1;