我有两张桌子:
Appointments (CustomerID,AppointmentID,SalesRepID,Status,AppointmentDate)
ResultedSales (CustomerID,AppointmentID,ResultedDate)
我正在寻找约会中的记录,其中:
- 结果状态(与待处理、已取消、打开等相反)
- 客户之前已出售给(ResultedSales 中的 CustomerID)
- 约会未作为销售结果(AppointmentID 不在 ResultedSales 中)
- 约会发生在客户第一次被出售之后
(AppointmentDate > AppointmentDate ResultedSales 中该 CustomerID 的最小 AppointmentID 记录) - 分配给预约的 SalesRep 与之前的销售相同
(SalesRepID = 该 CustomerID 的 ResultedSales 中任何 AppointmentID 记录的 SalesRepID)
前三个是通过
Select Distinct .AppointmentID from Appointments A
join ResultedSales RS on A.CustomerID=RS.CustomerID
Where A.StatusID='resulted'
And A.CustomerID in (Select CustomerIDfrom ResultedSales)
And A.AppointmentID Not in (select AppointmentID from ResultedSales)
但我不知道如何实现#4 和#5
任何帮助/提示?