学生表
Student_ID School Home State Grade Age
85 Washington St Colorado Junior 22
90 Washington St Washington Senior 23
81 Oregon California Junior 21
21 Washington Washington Sophomore 21考勤表
Student_ID Active Date
85 N 9/22/20
85 N 9/21/20
81 Y 9/22/20
81 N 9/21/20嘿,在Oracle中,如果我想用学生的信息清理表,看看谁仍然是活跃的学生。通过在考勤表中按Student_ID进行排序,如果学生有active = 'N‘的所有值,我希望找到他们。如果每个学生的所有值都是活动= 'N‘,那么我知道他们不再是学生了,我想从“学生”表(学生85)中删除这些记录。但是,如果只记录到每个学生的活动= 'Y',那么我就不会删除那个学生的任何内容,因为我知道他们仍然很活跃(学生81)。做这件事的最好方法是什么,我试过使用all操作符,但是我一直无法得到预期的结果。下面是我一直试图使用的查询。
DELETE /*+ parallel (a) */ FROM STUDENT a
WHERE ( a.student_ID = ALL
(SELECT /*+ parallel (b) */ b.student_id, b.active FROM attendance b WHERE b.active = 'N')); 发布于 2020-09-22 15:46:36
一个选项使用not exists
delete from student s
where not exists (
select 1 from attendance a where a.student_id = s.student_id and a.active = 'Y'
)这也会删除根本没有出勤率的学生。如果这不是您想要的,那么您可以使用关联子查询来代替:
delete from student s
where (
select min(active) from attendance a where a.student_id = s.student_id
) = 'N'发布于 2020-09-22 15:48:48
您可以使用聚合检查'Y‘行:
DELETE
FROM STUDENT
WHERE student_ID IN
(
SELECT student_id
FROM attendance
GROUP BY student_id
HAVING MAX(active) = 'N' -- no Y for this student
); 发布于 2020-09-22 16:20:52
NOT Exists 这将是最好的选择,因为猜测你必须处理一些巨大的桌子。我第二次查询GMB的第一个问题。
https://stackoverflow.com/questions/64013095
复制相似问题