Sql 不同、计数、分组查询疯狂
我试图返回每个学期的考试次数。我可以让计数返回,但无法按项对其进行分组 我尝试了所有方法,最接近的方法是按术语分组,但我的计数仅为=1,这是不对的 这是我现在拥有的。它只返回一个计数,如何按项\u id对其进行分组Sql 不同、计数、分组查询疯狂,sql,postgresql,aggregate-functions,Sql,Postgresql,Aggregate Functions,我试图返回每个学期的考试次数。我可以让计数返回,但无法按项对其进行分组 我尝试了所有方法,最接近的方法是按术语分组,但我的计数仅为=1,这是不对的 这是我现在拥有的。它只返回一个计数,如何按项\u id对其进行分组 SELECT COUNT(*) FROM (SELECT DISTINCT ON(student_id, test_event_id, terf.term_id) student_id FROM report.test_event_result_fact
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
在不了解更多设置的情况下,这是我的最佳选择:
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;
在不了解更多设置的情况下,这是我的最佳选择:
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;
一条建议-阅读越容易,每个学期就越有可能获得帮助测试?或者每学期每个学生的测试?提示:摆脱所有不相关的连接,以最简单的形式提出你的问题/问题,那么人们将更有可能帮助你一条建议——阅读越容易,每学期获得帮助测试的可能性越大?或者每个学生每个学期的测试?提示:摆脱所有不相关的连接,以最简单的形式提出你的问题,那么人们会更愿意帮助你