SQL query question: SELECT … NOT IN

前端 未结 7 1174

I am sure making a silly mistake but I can\'t figure what:

In SQL Server 2005 I am trying select all customers except those who have made a reservation before 2 AM.<

7条回答
  •  眼角桃花
    2020-12-24 05:47

    SELECT Reservations.idCustomer FROM Reservations (nolock)
    LEFT OUTER JOIN @reservations ExcludedReservations (nolock) ON Reservations.idCustomer=ExcludedReservations.idCustomer AND DATEPART(hour, ExcludedReservations.insertDate) < 2
    WHERE ExcludedReservations.idCustomer IS NULL AND Reservations.idCustomer IS NOT NULL
    GROUP BY Reservations.idCustomer
    

    [Update: Added additional criteria to handle idCustomer being NULL, which was apparently the main issue the original poster had]

提交回复
热议问题