看看這對你的作品
Select * From
(
Select
d.dealID
, do.doctorID
, dbo.fn_latlongdist($userlat, $userlong, do.doctorLatitude, do.doctorLongitude) as Distance
, ROW_NUMBER() OVER (PARTITION BY d.dealID ORDER BY dbo.fn_latlongdist($userlat, $userlong, do.doctorLatitude, do.doctorLongitude)) AS RowNumber
From
y_Deals d
JOIN
y_deals_doctor dd ON dd.dealID = d.dealID
JOIN
Doctor do on dd.doctorID = do.doctorID
) T
Where
T.RowNumber = 1
UPDATE:
;With AllData As
(
Select
d.dealID
, do.doctorID
, dbo.fn_latlongdist($userlat, $userlong, do.doctorLatitude, do.doctorLongitude) as Distance
, ROW_NUMBER() OVER (PARTITION BY d.dealID ORDER BY dbo.fn_latlongdist($userlat, $userlong, do.doctorLatitude, do.doctorLongitude)) AS RowNumber
From
y_Deals d
JOIN
y_deals_doctor dd ON dd.dealID = d.dealID
JOIN
Doctor do on dd.doctorID = do.doctorID
)
,DrCount As
(
Select
dealID
, Count(Distinct doctorID) as doctorCount
From
AllData
Group By
dealID
)
Select
*
From
AllData A
Inner Join
DrCount C
On
A.dealID = C.dealID
Where
A.RowNumber = 1
難道你不能只選擇頂行嗎?選擇Top 1 ... – 2011-03-25 17:09:12
醫生表是否包含有關其位置的一些信息? – Leslie 2011-03-25 17:09:14
@amit_g我不想要只有頂部1,我想要一個距離交易列表,但我想知道最接近的每個醫生 – DannyLeavitt 2011-03-25 17:11:41