简体   繁体   English

不同,计数,按查询疯狂分组

[英]Distinct, count, group by query madness

I am trying to return a count of tests taken per term. 我正在尝试返回每个学期参加的考试数量。 I can get the count to return, but I can't get it grouped by term. 我可以归还计数,但不能按学期分组。

I've tried everything and the closest I get is grouping by term but then my count only = 1, which isn't right. 我已经尝试了所有方法,但得到的最接近的结果是按学期分组,但后来我的计数只有= 1,这是不对的。

Here is what I have now. 这就是我现在所拥有的。 It just returns a count, how do I group it by term_id? 它只返回一个计数,如何按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

Without knowing more about your setup, that's my best bet: 在不了解您的设置的情况下,这是我最好的选择:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM