一个SQL算法问题求助!!!!

dmdjery 2002-12-28 11:51:57
各位同行兄弟朋友:

我有一个张表结构:(表名字:census)
字段:
id --记录的号码
x --坐标x的值
y --坐标y值
groupid --该记录所属的类型

现在要写一个存储过程:

CREATE PROCEDURE sp_census3a
@ax1 int,
@ay1 int,
@ax2 int,
@ay2 int
AS
declare @lrectx1 int
declare @lrecty1 int
declare @lrectx2 int
declare @lrecty2 int

set @lrectx1 = @ax1 - @ax2
set @lrectx2 = @ax1 + @ax2
set @lrecty1 = @ay1 - @ay2
set @lrecty2 = @ay1 + @ay2


select x,y,id,groupid
From
(select * from census
where @lrectx1<x and x<@lrectx2 and @lrecty1<y and y<@lrecty2) A
Group By x,y,id,GroupID
order by groupid



以上得出一个结果集,是以groupid来进行分组的。
现在要求:在以上每个分组中,

如果要求,
在每个小组中,找出一行组成新的
结果集,使得该结果集中:

任意2行x,y 列的值(xi,yi),(xj,yj) --xi表示第i行的x列的值,其他一样)
有:square(xi-xj)+square(yi-yj)>@const
其中@const为一个常数


这个要求一样在存储过程里实现,如何实现?








...全文
55 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dmdjery 2003-01-02
  • 打赏
  • 举报
回复
谢谢!!
dmdjery 2002-12-30
  • 打赏
  • 举报
回复
谢谢你的回复。
如果是这样的,你的解答能满足我要的那个结果集吗?

希望能和你邮件联系。谢谢!
samuelpan 2002-12-30
  • 打赏
  • 举报
回复
还没有做完

将上面取数导入#tt

select a.x x1, a.y y1, a.id id1, a.groupid groupid1,
b.x x2, b.y y2, b.id id2, b.groupid groupid2
into #tt
from #t a, #t b where a.groupid <> b.groupid
and (square(x1-x2)+square(y1-y2))>@const

select x1 x,y1 y,id1 id,groupid1 groupid from #tt
union all
select x2, y2, id2, groupid2 from #tt
samuelpan 2002-12-30
  • 打赏
  • 举报
回复
select x,y,id,groupid into #t
From
(select * from census
where @lrectx1<x and x<@lrectx2 and @lrecty1<y and y<@lrecty2) A
Group By x,y,id,GroupID
order by groupid

我们就从#t里面开始取数。
select a.x x1, a.y y1, a.id id1, a.groupid groupid1,
b.x x2, b.y y2, b.id id2, b.groupid groupid2
from #t a, #t b where a.groupid <> b.groupid
and (square(x1-x2)+square(y1-y2))>@const

j9988 2002-12-28
  • 打赏
  • 举报
回复
select x,y,id,groupid into #t
From
(select * from census
where @lrectx1<x and x<@lrectx2 and @lrecty1<y and y<@lrecty2) A
Group By x,y,id,GroupID
order by groupid

--是这样吗?
select * from #t A where not exists(select 1 from #t
where (x<>A.X or A.y<>Y) and square(a.x-x)+square(a.y-y)<=@const and groupid=A.groupid)

22,297

社区成员

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

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