回复 楼主 xiangyue0510
select min(([1].[X]-[2].[X])*([1].[X]-[2].[X])+([1].[Y]-[2].[Y]) * ([1].[Y]-[2].[Y])*0.01*3.14*3.14/180/180) FROM [1], [2]
这一句有问题:这个语句是把表1,表2组成的新表里面,这个表达式的最小值输出,
然后你再从表1,表2组成的新表里面去查,这个表达式的值 = 这个最小值时,所对应的XY坐标,这显然是错误的。
应该先将表1,表2组成的新表(假设表3)中按照表1的编号分组,求出每个编号组的最小值及对应的编号,然后再从表3中查找,
表达式 = 最小值,且编号相等。
程序代码:
select T1.T1_1_X as "1.X", T1.T1_1_Y as "1.Y", T1.T1_2_X as "2.X", T1.T1_2_Y "2.Y"
from (select [1].ID as "T1_ID", [1].X as "T1_1_X", [1].Y as "T1_1_Y", [2].X as "T1_2_X", [2].Y as "T1_2_Y" from [1], [2]) as "T1" ,
(select [1].ID as "T2_ID", MIN(([1].X-[2].X)*([1].X-[2].X) + ([1].Y-[2].Y)*([1].Y-[2].Y)* 0.01*3.14*3.14/180/180 ) as "T2_Min"
from [1], [2] group by [1].ID) as "T2"
where (T1.T1_1_X-T1.T1_2_X)*(T1.T1_1_X-T1.T1_2_X) + (T1.T1_1_Y-T1.T1_2_Y)*(T1.T1_1_Y-T1.T1_2_Y)* 0.01*3.14*3.14/180/180 = T2.T2_Min and T1.T1_ID = T2.T2_ID
如果用内连接会更简便一点
程序代码:
select [1].X, [1].Y, [2].X, [2].Y
from [1]
join [2]
on 1=1
join (select [1].ID as "1_ID", MIN(([1].X-[2].X)*([1].X-[2].X) + ([1].Y-[2].Y)*([1].Y-[2].Y)* 0.01*3.14*3.14/180/180 ) as "miniux"
from [1], [2] group by [1].ID) as "T"
on ([1].X-[2].X)*([1].X-[2].X) + ([1].Y-[2].Y)*([1].Y-[2].Y)* 0.01*3.14*3.14/180/180 = T.miniux and [1].ID = T.[1_ID]