我的数据表里有点的平面坐标(x,y),怎么用一个select语句来查找表里与已知点最接近的点

解决方案 »

  1.   

    create table t_1
    (x numeric(18,2),
    y numeric(18,2)
    )insert into t_1 values(1,2)
    insert into t_1 values(2,3)
    insert into t_1 values(3,5)
    insert into t_1 values(1,5)
    insert into t_1 values(3,2)
    select top 1 b.x, b.y
    from t_1 a,
    t_1 b
    where a.x = 1
      and a.y = 2
      and (b.x <> a.x or b.y <> a.y)
    order by (b.y-a.y)*(b.y-a.y) + (b.x-a.x)*(b.x-a.x)drop table t_1-----
    x                    y                    
    -------------------- -------------------- 
    2.00                 3.00(1 row(s) affected)
      

  2.   

    离(3,3)最小:
    select * from [Table] where square(a-3)+square(b-3)=(select min(square(a-3)+square(b-3)) from [Table])
      

  3.   

    知道x,y坐标最小增量吧,个x加一个增量值,然后判断(x+增量,y)存在不,如果不存在继续给y加,如果还没有继续,当然可以减一个增量值。如果x+增量>表中x最大值,停止对x值进行增加,如果y+增量>表中y最大值
      

  4.   

    create table t_1(x numeric(18,2),y numeric(18,2))insert into t_1 values(1,2)
    insert into t_1 values(2,3)
    insert into t_1 values(3,5)
    insert into t_1 values(1,5)
    insert into t_1 values(3,2)declare @x numeric(18,2),@y numeric(18,2) 
    select @x = 2,@y = 2select top 1 *,power((x-@x)*(x-@x)+(y-@y)*(y-@y),0.5) as 距离
    from t_1
    order by 距离drop table t_1
    /*
    x                    y                    距离                                       
    -------------------- -------------------- ---------------------------------------- 
    2.00                 3.00                 1.0000(1 row(s) affected)
    */
      

  5.   


    freeliu()數據create table t_1
    (x numeric(18,2),
    y numeric(18,2)
    )insert into t_1 values(1,2)
    insert into t_1 values(2,3)
    insert into t_1 values(3,5)
    insert into t_1 values(1,5)
    insert into t_1 values(3,2)select top 1* from t_1 order by (sqrt((x-3)*(x-3)+(y-3)*(y-3))) x                    y                    
    -------------------- -------------------- 
    2.00                 3.00(1 row(s) affected)
      

  6.   

    (1,2)和(2,1)到(2,2)的距离是一样的,top 1只能得到一条记录
      

  7.   

    create table t_1
    (x numeric(18,2),
          y numeric(18,2)
    )insert into t_1 values(1,2)
    insert into t_1 values(2,3)
    insert into t_1 values(3,5)
    insert into t_1 values(1,5)
    insert into t_1 values(3,2)declare @x numeric(18,2),@y numeric(18,2) 
    select @x = 4,@y = 5select top 1 x,y from (select x,y,jl = SQRT(SQUARE(x-@x) +SQUARE(y-@y)) from t_1)A order by A.jl
    drop table t_1
      

  8.   

    再改下把所有相等距離的記錄找出來
    select *,(sqrt((x-3)*(x-3)+(y-3)*(y-3)))as 距離 from t_1
    where (sqrt((x-3)*(x-3)+(y-3)*(y-3)))in
    (
    select (sqrt((x-3)*(x-3)+(y-3)*(y-3))) from t_1 
    group by (sqrt((x-3)*(x-3)+(y-3)*(y-3)))
    having count(*)>1
    )x                    y                    距離                                                    
    -------------------- -------------------- ----------------------------------------------------- 
    2.00                 3.00                 1.0
    3.00                 2.00                 1.0(2 row(s) affected)
      

  9.   

    不好意思,上面這條語句不行select *,(sqrt((x-3)*(x-3)+(y-3)*(y-3)))as 距離 from t_1
    where  sqrt((x-3)*(x-3)+(y-3)*(y-3))=(select top 1 sqrt((x-3)*(x-3)+(y-3)*(y-3)) from t_1 order by sqrt((x-3)*(x-3)+(y-3)*(y-3)) )x                    y                    距離                                                    
    -------------------- -------------------- ----------------------------------------------------- 
    2.00                 3.00                 1.0
    3.00                 2.00                 1.0(2 row(s) affected)
    這樣就可以了