我正在尝试加入一个内部查询,它的结果是由另一个表中的值过滤的,所以我可以选择最高结果并在我的主select语句中多次使用它,但是我收到如下错误:
无法绑定多部分标识符“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 ....
我怎样才能让它工作,所以我可以在多个字段的计算中使用我的查询中的值(我目前正在尝试加入),而不重复它?
答案 0 :(得分: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 ....
答案 1 :(得分: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”