来自多个" Not In"的查询不使用UNION的查询

时间:2015-05-07 18:32:35

标签: sql sql-server-2008 inner-join notin

很难为我所需要的东西赢得好头衔。

这围绕着3张桌子,是一个约会预约系统,我需要得到每个医生/护士的清单以及他们自由的时间段。

我不想使用UNION,就像下面的第一个sql一样,这意味着我必须声明每个医生的姓名,我不想这样做。

表...

的UserDetails:

ID    Role   Surname   Clinic
1    Doctor   House      1
2    Doctor   Bob        1
3    Nurse    Smith      1
4    Doctor   Jim        2
5    Nurse    Grant      2 
6    Patient  Billy      1
7    Patient  Jones      1

时隙:

ID   TimeSlot
1     10:00
2     10:30
3     11:00
4     11:30
11    16:30
12    17:00

预约:

ID  StaffID   PatientID  TimeSlot   AppDate
1      1         6          1        today   
2      1         7          3        today
3      2         6          2        today
4      1         6          4       tomorrow

(StaffID和PatientID,是来自用户表的外键ID) 我需要一个输出每个时间段和Doc / Nurse的查询,以确定他们今天没有预约(或者基于" AppDate&#34的任何一天;)

我可以为特定的医生/护士做这件事:

SELECT TimeSlots.TimeSlot, Users.Role, Users.Surname, Users.Clinic 
FROM TimeSlots, Users 
WHERE 
    TimeSlots.ID NOT IN 
        (SELECT Appointments.TimeSlot
        FROM Appointments  
            INNER JOIN Users    
            ON Appointments.MedicalStaffID = Users.ID 
        WHERE AppDate = CONVERT(DATE,GETDATE()) AND Users.Surname = 'House')
AND
    Users.Surname = 'House'
ORDER BY TimeSlots.TimeSlot;

这给了我:

TimeSlot   Role   Surname    Clinic
  10:30   Doctor   House       1
  11:30   Doctor   House       1
  16:30   Doctor   House       1
  17:00   Doctor   House       1

哪个好,但我需要1个查询才能显示给所有医生/护士,所以我有:

   TimeSlot   Role   Surname    Clinic
     10:30   Doctor   House       1
     11:30   Doctor   House       1
     16:30   Doctor   House       1
     17:00   Doctor   House       1
     10:00   Doctor   Bob         1
     11:00   Doctor   Bob         1
     11:30   Doctor   Bob         1
     16:30   Doctor   Bob         1
     17:00   Doctor   Bob         1

所以它也可以按时排序。

我最初尝试过:

SELECT TimeSlots.TimeSlot, Users.Role, Users.Surname, Users.Clinic 
FROM TimeSlots, Users 
WHERE 
    TimeSlots.ID NOT IN (SELECT TimeSlot FROM Appointments  WHERE AppDate = GETDATE() AND (Users.Clinic = 'Werrington') AND (Users.Role = 'Doctor' OR Users.Role = 'Nurse'))
AND
    (Users.Role = 'Doctor' OR Users.Role = 'Nurse')
AND
    (Users.Clinic = 'Werrington')
ORDER BY TimeSlots.TimeSlot;

但这只是每个时间段输出每个医生

从旁注来看,我觉得这是一种更好的方法来构建结果表的外观,不能想到如何。

1 个答案:

答案 0 :(得分:2)

如果我正确地理解了这个问题,这应该会给你一些你想要的东西......

SET NOCOUNT ON;

DECLARE @UserDetails TABLE (
    ID      int,
    Role    varchar(50),
    Surname varchar(50),
    Clinic  int )

INSERT @UserDetails VALUES (1, 'Doctor', 'House', 1)
INSERT @UserDetails VALUES (2, 'Doctor', 'Bob', 1)
INSERT @UserDetails VALUES (3, 'Nurse', 'Smith', 1)
INSERT @UserDetails VALUES (4, 'Doctor', 'Jim', 2)
INSERT @UserDetails VALUES (5, 'Nurse', 'Grant', 2 )
INSERT @UserDetails VALUES (6, 'Patient', 'Billy', 1)
INSERT @UserDetails VALUES (7, 'Patient', 'Jones', 1)

DECLARE @TimeSlots TABLE (
    ID          int,
    TimeSlot    varchar(50) )

INSERT @TimeSlots VALUES (1 , '10:00' )
INSERT @TimeSlots VALUES (2 , '10:30' )
INSERT @TimeSlots VALUES (3 , '11:00' )
INSERT @TimeSlots VALUES (4 , '11:30' )
INSERT @TimeSlots VALUES (11, '16:30' )
INSERT @TimeSlots VALUES (12, '17:00' )

DECLARE @Appointments TABLE (
    ID          int,
    StaffID     int,
    PatientID   int,
    TimeSlotID  int,
    AppDate     varchar(50) )

INSERT @Appointments VALUES (1, 1, 6, 1, 'today' ) 
INSERT @Appointments VALUES (2, 1, 7, 3, 'today' )
INSERT @Appointments VALUES (3, 2, 6, 2, 'today' )
INSERT @Appointments VALUES (4, 1, 6, 4, 'tomorrow' )

SET NOCOUNT OFF;

WITH CompleteSchedule AS (
    SELECT      UD.ID as UserID, UD.Role, UD.Surname, UD.Clinic, TS.ID as TimeSlotID, TS.TimeSlot
    FROM        @UserDetails UD
    CROSS JOIN  @TimeSlots TS
)
SELECT      CS.*
FROM        CompleteSchedule CS
LEFT JOIN   @Appointments A ON A.StaffID = CS.UserID AND A.TimeSlotID = CS.TimeSlotID AND A.AppDate = 'today'
WHERE       A.ID is null
ORDER BY    CS.UserID, CS.TimeSlotID

CTE将在每个时段生成每个工作人员X的“表格”。然后你左右加入指定日期的任命。任何没有约会ID的结果行都是打开的时间段。