2016-04-26 110 views
0

我正在嘗試使用Oracle執行SQL分區,因此應該返回所有User_ID將SAME類作爲特定ID教授的信息。執行分區的SQL查詢

兩個表的表結構,我的工作:

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 

查詢應:

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) 

查詢我目前正在工作返回instructor_id 2,因爲它是唯一指示相同類別的人instructor_id 1

儘管插入數據以匹配此場景,但我已經返回任何行。

+0

是什麼「同一類」的意思是 - 相同的類代碼或相同的類的數量,或兩者?在你的示例中,這兩列之間存在一對一的關係,但大概在現實生活中,情況並非如此(可能是class_num反映了課程代碼和學期?或者位置? - 否則爲什麼都有?) – mathguy

+0

相同的類的意思,class_code和class_num – hello

回答

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酒吧),它們給予與Foo酒吧相同的課程。

如果你想排除美孚吧,只需添加另一個WHERE類(instructor_id!=(SELECT HUMAN.id從人WHERE第一= '富' 和LAST = '酒吧'))

+0

這個查詢返回所有'instructor_id',它指示至少一個由'foo bar'指示的類。 – hello

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 
+0

如果我改變了John Doe指示的一個類,意思是說,它不再與Foo Bar指示的所有類匹配,我仍然得到John Doe,它應該返回空行。 – hello

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')); 
-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)); 
+0

從未聽過 - 這是做你需要的嗎? – mathguy