1

我正在尝试加入一个内部查询,该查询的结果由另一个表中的值过滤,因此我可以选择顶部结果并在我的主选择语句中多次使用其中的值,但我得到了错误如下:

无法绑定多部分标识符“TessSCCall.Call_Num”。

到目前为止,请参见下面的代码:

SELECT BestAppointmentOffer AS foo -- I'm using this lots of times in different fields
        BestAppointmentOffer AS bar -- I'm using this lots of times in different fields
    ....
FROM TessSCEmploy 
INNER JOIN TessSCCall on TessSCEmploy.Employ_Num = TessSCCall.Call_Employ_Num
INNER JOIN
(
   SELECT TOP 1 dbo.Aqua_Midnight(AppointmentStartTime) 
     AS BestAppointmentOffer,    CallNumber
   FROM AQWEB.[360Tracking].dbo.AppointmentOffers
   WHERE CallNumber = TessSCCall.Call_Num
   ORDER BY AppointmentStartTime) AS Appointment 
 on TessSCCall.Call_Num = Appointment.CallNumber
where ....

我怎样才能让它工作,所以我可以在多个字段的计算中使用我的查询(我目前正在尝试加入)中的值,而不重复它?

4

2 回答 2

2

最简单的方法是使用CROSS APPLY,但由于您使用的是 SQL Server 2000,因此您没有该选项。您应该能够使用聚合函数来获得每个约会时间的最佳结果:

select BestAppointmentOffer AS foo -- I'm using this lots of times in different fields
        BestAppointmentOffer AS bar -- I'm using this lots of times in different fields
    ....
from TessSCEmploy 
inner join TessSCCall 
  on TessSCEmploy.Employ_Num = TessSCCall.Call_Employ_Num
INNER JOIN 
(
  SELECT min(dbo.Aqua_Midnight(AppointmentStartTime)) AS BestAppointmentOffer, 
    CallNumber
  FROM AQWEB.[360Tracking].dbo.AppointmentOffers
  GROUP BY CallNumber
) AS Appointment 
  on TessSCCall.Call_Num = Appointment.CallNumber
where ....
于 2013-05-16T11:05:29.893 回答
0

您需要 CROSS APPLY 关联派生表中的列

...

TessSCEmploy inner join TessSCCall on TessSCEmploy.Employ_Num = TessSCCall.Call_Employ_Num

CROSS APPLY
(SELECT TOP 1 dbo.Aqua_Midnight(AppointmentStartTime) AS BestAppointmentOffer, CallNumber
 FROM AQWEB.[360Tracking].dbo.AppointmentOffers
 WHERE CallNumber = TessSCCall.Call_Num
 ORDER BY AppointmentStartTime) AS Appointment on TessSCCall.Call_Num = Appointment.CallNumber

where ....

无论如何,CROSS APPLY 都是正确的构造,它是“每件事的 TOP 1”

于 2013-05-16T11:04:26.677 回答