从学生 table 中获取学生姓名,该学生属于少于 5 名学生的学区
Getting student name from student table belonging to a section with less than 5 students enrolled
大家好,我有一个问题要问你们。您可以在下图中看到注册和学生 table。我想 运行 从学生 table 获取名字和姓氏的查询,但学生应该属于少于 5 个注册学生的部分。
这有意义吗?如果我不清楚,请问我一个问题。非常感谢任何帮助。
这应该有效。
SELECT s.FIRST_NAME, s.LAST_NAME
FROM student s
WHERE s.STUDENT_ID IN (
SELECT e1.STUDENT_ID
FROM e1.enrollment
WHERE e1.SECTION_ID IN (
SELECT e2.SECTION_ID
FROM e2.enrollment
GROUP BY e2.SECTION_ID HAVING COUNT(DISTINCT e2.STUDENT_ID) < 5
)
)
select t2.first_name, t2.last_name
from table2 t2
inner join
(select student_id from table1 where section_id in
(select section_id from
(select section_id, count(student_id)
from table1
group by section_id
having count(student_id) < 5
)
)
)t1
on t1.student_id = t2.student_id;
它不漂亮,但应该可以。
为什么要多次访问一个 table,如果使用分析函数可以节省一些工作?
select
first_name, last_name
from
(
select
s.first_name, s.last_name, count(*) over(partition by e.section_id) as enrollment_count
from
student s
join enrollment e using (student_id)
)
where
enrollment_count < 5;
大家好,我有一个问题要问你们。您可以在下图中看到注册和学生 table。我想 运行 从学生 table 获取名字和姓氏的查询,但学生应该属于少于 5 个注册学生的部分。
这有意义吗?如果我不清楚,请问我一个问题。非常感谢任何帮助。
这应该有效。
SELECT s.FIRST_NAME, s.LAST_NAME
FROM student s
WHERE s.STUDENT_ID IN (
SELECT e1.STUDENT_ID
FROM e1.enrollment
WHERE e1.SECTION_ID IN (
SELECT e2.SECTION_ID
FROM e2.enrollment
GROUP BY e2.SECTION_ID HAVING COUNT(DISTINCT e2.STUDENT_ID) < 5
)
)
select t2.first_name, t2.last_name
from table2 t2
inner join
(select student_id from table1 where section_id in
(select section_id from
(select section_id, count(student_id)
from table1
group by section_id
having count(student_id) < 5
)
)
)t1
on t1.student_id = t2.student_id;
它不漂亮,但应该可以。
为什么要多次访问一个 table,如果使用分析函数可以节省一些工作?
select
first_name, last_name
from
(
select
s.first_name, s.last_name, count(*) over(partition by e.section_id) as enrollment_count
from
student s
join enrollment e using (student_id)
)
where
enrollment_count < 5;