我有这些数据:
IF OBJECT_ID('tempdb..#temp') IS NOT NULL
DROP TABLE #temp
CREATE TABLE #temp
(
Id INT IDENTITY(1, 1) ,
X FLOAT NOT NULL ,
Y FLOAT NOT NULL
)
INSERT INTO #temp (X, Y) VALUES (0, 0)
INSERT INTO #temp (X, Y) VALUES (0, 1)
INSERT INTO #temp (X, Y) VALUES (0, 2)
INSERT INTO #temp (X, Y) VALUES (0.5, 1)
INSERT INTO #temp (X, Y) VALUES (1, 1)
INSERT INTO #temp (X, Y) VALUES (1, 2)
INSERT INTO #temp (X, Y) VALUES (1.5, 0.5)
INSERT INTO #temp (X, Y) VALUES (2, 0)
INSERT INTO #temp (X, Y) VALUES (2, 1)
我想删除其他点中包含的点,例如:
(0, 1)
(1, 1)
(1.5, 0.5)
获得定义外部多边形的最外部点,该外部多边形仅包括没有冗余的垂直和水平线(例如(0,1)是冗余点)。这可以通过SQL Server 2014中基于集合的TSQL方法实现吗?
PS:
数据的散点图如下:
我想删除包围点。最终,我在外边界(绘制为红线)之后。希望这更清楚。
答案 0 :(得分:1)
我相信这可行。它似乎可以提供您的测试数据。 有点粗糙。如果您的实际数据很大,可能会提前计算一些SELECT MIN和SELECT MAX。
SELECT *
-- uncomment this to delete the desired points
-- DELETE #temp
FROM #temp t
WHERE
(
-- Internal points
(
( X > (SELECT MIN(X) FROM #temp) AND X < (SELECT MAX(X) FROM #temp) )
AND ( Y > (SELECT MIN(Y) FROM #temp) AND Y < (SELECT MAX(Y) FROM #temp) )
)
-- Exceptions (points with nothing strictly outside them) [Don't want to lose (1,1)]
AND EXISTS (SELECT * FROM #temp WHERE X > t.X AND Y > t.Y)
)
OR
-- redundant edge points [(0,1) would be included as an "exception"]
(
( (t.X = (SELECT MIN(X) FROM #temp) OR t.X = (SELECT MAX(X) FROM #temp))
AND EXISTS (SELECT * FROM #temp WHERE X = t.X AND Y > t.Y)
AND EXISTS (SELECT * FROM #temp WHERE X = t.X AND Y < t.Y) )
OR
( (t.Y = (SELECT MIN(Y) FROM #temp) OR t.Y = (SELECT MAX(Y) FROM #temp))
AND EXISTS (SELECT * FROM #temp WHERE Y = t.Y AND X > t.X)
AND EXISTS (SELECT * FROM #temp WHERE Y = t.Y AND X < t.X) )
)