关于SQl中碰到的一个难点 高手进

老君的春天 2012-06-20 01:46:39
比如说一张坐标表中
id smx smy
1 1.1 1.5
2 2.2 2.6
3 3.3 3.7
4 4.4 6.0
5 5.5 6.1

2个点产生一条线

第一行是一个点 第二行是一个点

两个点是一条线
然后我最后要计算 第一行到第五行 形成的线段的长度
我用SQL语句应该怎么实现

坐标求线段公式为:d=V[(x1-x2)^2+(y1-y2)^2]

高分求助!
...全文
91 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([id] int,[smx] numeric(5,1),[smy] numeric(5,1))
insert [test]
select 1,1.1,1.5 union all
select 2,2.2,2.6 union all
select 3,3.3,3.7 union all
select 4,4.4,6.0 union all
select 5,5.5,6.1


--写一个存储过程,指定求任意两点的距离:
if OBJECT_ID('pro_test')is not null
drop proc pro_test
go
create proc pro_test
(
@StartPoint int,
@EndPoint int
)
as
declare @x1 numeric(5,1),@y1 numeric(5,1)
declare @x2 numeric(5,1),@y2 numeric(5,1)
select @x1=[smx],@y1=[smy] from test where id=@StartPoint
select @x2=[smx],@y2=[smy] from test where id=@EndPoint
declare @lenth numeric(5,2)
set @lenth=sqrt(SQUARE(@x1-@x2)+SQUARE(@y1-@y2))
select @lenth as 距离
go

exec pro_test 1,5
/*
距离
---------
6.37
*/

select sqrt(9)



天-笑 2012-06-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
引用 1 楼 的回复:
SQL code

--生成测试数据
;with T as (
select 1 as ID ,1.1 as smx,1.5 as smy union all
select 2 as ID ,2.2 as smx,2.6 as smy union all
select 3 as ID ,3.3 as smx,3.7 as smy union all
sele……
[/Quote]

那你建个表吧,把几万条数据线插入
老君的春天 2012-06-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
SQL code

--生成测试数据
;with T as (
select 1 as ID ,1.1 as smx,1.5 as smy union all
select 2 as ID ,2.2 as smx,2.6 as smy union all
select 3 as ID ,3.3 as smx,3.7 as smy union all
select 4 as ID ,……
[/Quote]




那如果是几千条数据几万条数据 需要测试
如何进行
老君的春天 2012-06-20
  • 打赏
  • 举报
回复
那如果是几千条数据几万条数据 需要测试
如何进行
天-笑 2012-06-20
  • 打赏
  • 举报
回复

--生成测试数据
;with T as (
select 1 as ID ,1.1 as smx,1.5 as smy union all
select 2 as ID ,2.2 as smx,2.6 as smy union all
select 3 as ID ,3.3 as smx,3.7 as smy union all
select 4 as ID ,4.4 as smx,6.0 as smy union all
select 5 as ID ,5.5 as smx,6.1 as smy
),T2 as
(
select a.*,b.smx as smx2,b.smy as smy2 from t a inner join t b on a.id = b.id -1

)
--求线段距离
--select * , SQRT(SQUARE(SMX2-SMX) + SQUARE(SMY2-SMY)) as [线段距离]
--from T2
--求总距离
select sum(SQRT(SQUARE(SMX2-SMX) + SQUARE(SMY2-SMY)) )as [总距离]
from T2


22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧