我有两张桌子:员工和约会。
1员工可以有多个约会。 有一个字段:可见性可以容纳0,1或2.
0: show any of the appointment,
1: Show this appointment only,
2: Don't show the appointment.
现在我想选择员工和约会的记录:
有人能指出我如何使用Sql Server 2000完成它吗?
答案 0 :(得分:0)
With SelectedAppointments AS
{
SELECT * FROM Appointments WHERE Visibility <> 2
}
SELECT Employee.Id, < All columns of SelectedAppointments unless EmployeeId>
FROM Employee LEFT JOIN SelectedAppointments
on Employee.Id = SelectedAppointments.EmployeeId
答案 1 :(得分:0)
select *
from Employee as Emp
left outer join
(select top 1 *
from Appointments
where Visibility <> 2
order by Visibility desc) as App
on Emp.EmpID = App.EmpID
答案 2 :(得分:0)
我做了几个假设。
您只能拥有1条
的记录SELECT
e.empid,
COALESCE(OneAppoint.appointmentID, ZeroAppoint.appointmentID) appointmentID
FROM
employee e
LEFT JOIN appointment ZeroAppoint
INNER JOIN (
SELECT
max(a.appointmentid) appointmentid ,
a.EmpID
FROM
appointment a
WHERE
a.Visibility =0
) maxZeroAppoint
ON ZeroAppoint.appointmentid = maxZeroAppoint.appointmentid
ON e.empID = ZeroAppoint.empID
LEFT JOIN appointment OneAppoint
ON e.empID = OneAppoint.empID
and OneAppoint.Visibility = 1