让我们考虑(x,y)
是一个点,然后做一个radius
的一些值r
。如果假设我有一个点(a,b)
,我需要检查(a,b)
是否在以(x,y)
为中心的圆内。点存在于圆圈
如何在SQL Server中实现此目的?
让我们考虑(x,y)
是一个点,然后做一个radius
的一些值r
。如果假设我有一个点(a,b)
,我需要检查(a,b)
是否在以(x,y)
为中心的圆内。点存在于圆圈
如何在SQL Server中实现此目的?
测试数据
DECLARE @t TABLE (x NUMERIC(10,2), y NUMERIC(10,2), radius NUMERIC(10,2))
INSERT INTO @t
VALUES (3.5,3.5, 5.5),(20.5,20.5, 10.5), (30.5,30.5, 20.5)
查询
DECLARE @p1 NUMERIC(10,2) = 5.5 --<-- Point to check
DECLARE @p2 NUMERIC(10,2) = 5.5
SELECT *, CASE WHEN POWER(@p1 - x, 2) + POWER(@p2 - y, 2) <= POWER(radius, 2)
THEN 'Inside The Circle'
WHEN POWER(@p1 - x, 2) + POWER(@p2 - y, 2) > POWER(radius, 2)
THEN 'Outside the Circle' END [Inside/Outside]
FROM @t
结果集
╔═══════╦═══════╦════════╦════════════════════╗
║ x ║ y ║ radius ║ Inside/Outside ║
╠═══════╬═══════╬════════╬════════════════════╣
║ 3.50 ║ 3.50 ║ 5.50 ║ Inside The Circle ║
║ 20.50 ║ 20.50 ║ 10.50 ║ Outside the Circle ║
║ 30.50 ║ 30.50 ║ 20.50 ║ Outside the Circle ║
╚═══════╩═══════╩════════╩════════════════════╝
随着问题被关闭,再也吨添加其他的回答,所以我编辑此使用SQL Server Geometry
类型为包括溶液... [使用如上相同的数据点,加上一个精确演示圆上]
Declare @t TABLE
(x NUMERIC(10,2), y NUMERIC(10,2),
radius NUMERIC(10,2))
Insert @t
Values (3.5,3.5, 5.5),(20.5,20.5, 10.5),
(30.5,30.5, 20.5), (-5.5, 5.5, 11.0)
-- --------------------------
Declare @pX float = 5.5
Declare @pY float = 5.5
Declare @c geometry;
Declare @p geometry;
Select x, y, radius,
(geometry::Point(X, Y, 0)).STDistance(geometry::Point(@pX, @pY, 0))
From @T
Where (geometry::Point(X, Y, 0)).STDistance(geometry::Point(@pX, @pY, 0)) > radius
谢谢,工作正常。 –
没有问题很高兴它帮助:) –
@CharlesBrentana干杯朋友从来没有见过你的编辑,直到现在:)我从来没有在SQL服务器中使用这些'几何类型',绝对值得了解。 –
(XA)^ 2 +( yb)^ 2 <= r^2 –
这个问题是数学,而不是编程 –
@CharlesBretana他正在问一个SQL查询,因为他没有提供任何他自己尝试过的东西, –