Oracle Relational Division Query

时间:2017-12-04 08:34:42

标签: sql oracle relational-division

我正在尝试编写一个Relational Division查询(我需要使用Relational Division) 鉴于以下表格:

嘉宾:

   GID      PHNUM FNAME           LNAME                CCNUM RESDATE                                                                                                                                                         
  1654 3132565356 Tyrone          Biggums               8420 2016-11-04                                                                           
  1743 2484352574 Noah            Colon                 3345 2016-11-03                                                                           
  1501 2487615478 Brennan         Luttrell              6426 2016-11-05   

间:

ROOMNUM ROOMTYPE          GID                                                                                                                      

   101 Suite            1654                                                                                                                      
   103 Suite                                                                                                                                      
   105 Suite            1743                                                                                                                      
   107 Suite                                                                                                                                      
   108 Double           1501                                                                                                                      
   115 Double           1654                                                                                                                      
   117 Double                                                                                                                                     
   130 Single           1501                                                                                                                      
   135 Single           1501  

员工:

SSN       SUPERSSN  FNAME           LNAME                PHNUM       WAGE                                                                                                                      
123456789           Jim             Lahey           2464572367         40                                                               
532674246 123456789 Geno            Bianco          2463601456         15                                                             
837264268 123456789 Randy           Bobandy         2463603678         10                                                                
738643653 837264268 Corey           Trevor          2464326734          7                                                                 
648753674 837264268 Joshua          Trussler        2464327535          8 

维护:

EMPLOYEES    ROOMNUM                                                                                                                                                                                                                                                             
648753674        101                                                                                                                                  
648753674        115                                                                                                                                  
738643653        101                                                                                                                                  
738643653        105                                                                                                                                  
738643653        108                                                                                                                                  
738643653        130                                                                                                                                  
738643653        135                                                                                                                                  
837264268        107      

我正在尝试编写一个查询,该查询将使用关系部门来选择每个清理GID = 1654的客人保留的每个房间的员工

我写的查询如下:

SELECT E.ssn, E.lname, E.fname
FROM employee E
WHERE NOT EXISTS((SELECT G.gid
                  FROM guest G
                  WHERE G.gid = 1654)
                  MINUS
                  (SELECT G.gid
                   FROM maintains M, room R, guest G
                   WHERE G.gid = R.gid
                   AND R.roomnum = M.roomnum
                   AND E.ssn = M.employeessn
                   AND G.gid = 1654));

然而,在它的当前形式中,它返回以下内容,这是不正确的:

SSN       LNAME           FNAME                                                                                                                                                                                                                                 
738643653 Trevor          Corey                                                                                                                       
648753674 Trussler        Joshua       

感谢您的所有帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

您可以统计员工维护的所有房间和房间,并且只采用具有相同值的行:

select distinct ssn, fname, lname
  from (select e.ssn, e.fname, e.lname, 
               count(distinct r.roomnum) over () cnt_room, 
               count(1) over (partition by e.ssn) cnt_ssn
          from room r
          join maintains m on m.roomnum = r.roomnum
          join employee e  on e.ssn = m.employeessn
          where gid = 1654)
  where cnt_room = cnt_ssn

<强> SQL Fiddle Demo

结果:

       SSN FNAME  LNAME
---------- ------ --------
 648753674 Joshua Trussler