在sql server 2005中,我有一个描述路径的父表,以及一个包含每条路径上每个点的坐标的子表,如下所示:
int path_id, string path_name
int path_id, float x, float y, int n
路径上的第一个点是n = 1,然后添加的每个新点都会递增n。
我需要做的是,删除此返回的点:
select [path_id]
from Points
where n=1
即。每个路径中的第一个点,然后还删除所有的点,其中n是由此返回的值:
select [path_id], max(n)
from Points
group by [path_id]
即。每个路径的最后一个添加点。
对于第一部分,我可以这样做:
delete from Points
where n=1
但我该如何处理第二部分?
答案 0 :(得分:1)
DELETE FROM Points
FROM points
INNER JOIN
(SELECT point_id, path_id, n
FROM (SELECT ROW_NUMBER() OVER(PARTITION BY path_id ORDER BY n DESC) AS RowNumber,
point_id, path_id, n
FROM points ) t
WHERE n = 1 or RowNumber = 1
) t on t.point_id = points.point_id and t.path_id = points.path_id and t.n = points.n
答案 1 :(得分:0)
第二部分尝试这个:
DELETE from points where path_id in
(
Select path_id from (
select [path_id], max(n)
from Points
group by [path_id]
) t
)
答案 2 :(得分:0)
我认为这个答案不适用于SQL Server,而是说明如何使用IN运算符支持元组的数据库:
DELETE FROM Points
WHERE (path_id, n) IN
(SELECT path_id, max(n)
FROM Points
GROUP BY path_id)
答案 3 :(得分:0)
这是一种可以解决问题的方法:
create table dummy as (
select path_id, max(n)
from points
group by path_id
);
delete from points where n in
select n from dummy where points.path_id = dummy.path_id
);
drop table dummy;