我正在尝试使用Oracle执行SQL分区,因此,应该返回所有已将SAME类作为特定ID教授的User_ID
。
我正在使用的两个表的表结构:
HUMAN(id, first, last)
INSTRUCTOR(human_id, location) -- PK(id, location)
CLASS(instructor_id, class_code, class_num) -- PK(instructor_id, class_code, class_num)
我正在使用的查询:
SELECT DISTINCT instructor_id, class_code, class_num
FROM CLASS
WHERE NOT EXISTS (
(SELECT instructor_id, class_code, class_num FROM CLASS)
MINUS
(SELECT instructor_id, class_code, class_num
FROM CLASS
WHERE instructor_id =
(SELECT HUMAN.id
FROM HUMAN
WHERE first = 'Foo'
AND last = 'Bar')))
示例数据
HUMAN
id first last
1 foo bar
2 John Doe
INSTRUCTOR
human_id location
1 US
2 CA
CLASS
instructor_id class_code class_num
1 CS 999
1 MA 111
1 DE 222
2 CS 999
2 MA 111
2 DE 222
3 CS 999
4 CS 999
查询应该返回instructor_id 2
,因为它是唯一指示同一类instructor_id 1
尽管插入数据以匹配此场景,但我的查询没有返回任何行。
答案 0 :(得分:0)
SELECT DISTINCT instructor_id, class_code, class_num
FROM CLASS
WHERE class_num IN (SELECT class_num from CLASS WHERE instructor_id = (SELECT HUMAN.id FROM HUMAN WHERE first = 'Foo' AND last = 'Bar'))
这将返回所有教师(包括Foo Bar),这些教师提供与Foo Bar相同的课程。
如果要排除Foo Bar,只需添加另一个WHERE类(instructor_id!=(SELECT HUMAN.id FROM HUMAN WHERE first =' Foo' AND last =' Bar') )
答案 1 :(得分:0)
其中一种方式:
with t as (
select distinct instructor_id id, class_num cn
from class
where instructor_id = (select id from human
where first = 'foo' and last = 'bar'))
select c.instructor_id as id, max(h.first||' '||h.last) as name
from class c
join human h on c.instructor_id = h.id
join t on t.cn = c.class_num and c.instructor_id <> t.id
group by instructor_id
having count(distinct class_num) = (select count(1) from t)
测试数据和输出:
create table HUMAN (id number(3), first varchar2(5), last varchar2(5));
insert into human values (1, 'foo', 'bar');
insert into human values (2, 'John', 'Doe');
create table INSTRUCTOR (human_id number(3), location varchar2(3));
insert into instructor values (1, 'US');
insert into instructor values (2, 'CA');
create table CLASS (instructor_id number(3), class_code varchar2(3), class_num number(4));
insert into class values (1, 'CS', 999 );
insert into class values (1, 'MA', 111 );
insert into class values (1, 'DE', 222 );
insert into class values (2, 'CS', 999 );
insert into class values (2, 'MA', 111 );
insert into class values (2, 'DE', 222 );
insert into class values (3, 'CS', 999 );
insert into class values (4, 'CS', 999 );
输出:
ID NAME
---- -----------
2 John Doe
答案 2 :(得分:0)
这个怎么样:
SELECT *
FROM instructor i
WHERE NOT EXISTS
(SELECT *
FROM class c
WHERE c.instructor_id = i.human_id
MINUS
SELECT *
FROM class c
WHERE instructor_id = (SELECT human.id
FROM human
WHERE FIRST = 'Foo' AND LAST = 'Bar'));
答案 3 :(得分:-1)
我假设教师可以多次教授同一个班级(如果他们不能在某种程度上简化查询)。我还假设输入是作为“名字,姓氏”而不是直接作为human.id给出的,尽管这是一种运行查询的糟糕方式;如果多个教师具有相同的名字和姓氏怎么办?使用唯一标识符。
无论如何,这里是。可以对其进行修改以适应数据模型或输入模型的变化;现在它是基于OP所述的要求。
with sel(p_id) as (select id from human where first = 'foo' and last = 'bar'),
a(i, ct) as (select instructor_id, count(distinct class_code || class_num)
from class group by instructor_id)
select i from a join sel on i != p_id
where
(select count(distinct class_code || class_num) from class
where instructor_id in (p_id, i)) =
(select min(ct) from a a1 where a1.i in (p_id, a.i));