私は2つのテーブルを持っています:
Appointments (CustomerID,AppointmentID,SalesRepID,Status,AppointmentDate)
ResultedSales (CustomerID,AppointmentID,ResultedDate)
アポイントメントでレコードを探しています。
- ステータスが表示されます(保留中、キャンセル済み、オープンなどではなく)
- 顧客は以前に販売されました(ResultedSalesのCustomerID)
- Appointmentは販売として生成されませんでした(AppointmentIDはResultedSalesに含まれていません)
- Appointmentは、顧客が最初に販売された後に発生します
(AppointmentDate>そのCustomerIDのResultedSalesの最小のAppointmentIDレコードのAppointmentDate) - アポイントメントに割り当てられたSalesRepは、前の販売と同じです
(SalesRepID =そのCustomerIDのResultedSalesのAppointmentIDレコードのSalesRepID)
最初の3つはで達成されます
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を達成する方法を理解できません
ヘルプ/ヒントはありますか?