使用sql查找两点之间的坐标

时间:2019-04-26 13:50:26

标签: sql sql-server tsql

我有两列X列和Y列。(X1,Y1)=(1,2)和(X2,Y2)=(9,10)。以X1,Y1为起点,X2,Y2为终点,我可以找到Slope。但是使用斜率和这些点。我如何找到它们之间的剩余点

例如:我有类似的值

ColumnX  ColumnY
1         1
.         .
.         .
.         .
10        10

坡度为Y1-Y2 / X1-X2,即10-1 / 10-1 = 1

使用坡度和坐标,如何使用Sql查找它们之间的其余9个坐标

2 个答案:

答案 0 :(得分:0)

我不完全了解您的输入数据的样子,但是您可以使用递归CTE生成点:

with points as (
      select 1 as x_start, 2 as y_start, 9 as x_end, 10 as y_end
     ),
     cte as (
      select x_start as x, convert(float(53), y_start) as y, 9 as x_end, convert(float(53), (y_end - y_start) * 1.0 / (x_end - x_start)) as slope
      from points
      union all
      select x + 1, y + slope, x_end, slope
      from cte
      where x < x_end
     )
select *
from cte
order by x;

Here是db <>小提琴。

答案 1 :(得分:0)

Here is the solution,请告诉我它是否适合您

declare @x1 as decimal(10,2)
declare @y1 as decimal(10,2)
declare @x2 as decimal(10,2)
declare @y2 as decimal(10,2)

set @x1=1
set @y1=1
set @x2=10
set @y2=10


declare @mytab as table (x decimal(10,2),y decimal(10,2))

insert into @mytab values(@x1,@y1),(@x2,@y2)




declare @slope as decimal(10,2)
set @slope=(@y1-@y2)/(@x1-@x2)

--(y2=y1+s*(x2-x1)
;with cte as(
select @x1 x, @y1 y 
union all
select cast(x+1 as decimal(10,2)),cast( @y1+@slope*(x+1.0-@x1) as decimal(10,2)) from cte
where x+1 < 11)
select x,y from cte