gpt4 book ai didi

sql - 区分、计数、分组查询疯狂

转载 作者:行者123 更新时间:2023-11-29 13:36:55 24 4
gpt4 key购买 nike

我正在尝试返回每个学期参加的测试的计数。我可以得到要返回的计数,但无法按术语分组。

我已经尝试了所有方法,最接近的结果是按术语分组,但我的计数只有 1,这是不对的。

这是我现在拥有的。它只返回一个计数,我如何按 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

最佳答案

在不了解您的设置的情况下,这是我最好的选择:

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;

关于sql - 区分、计数、分组查询疯狂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7762642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com