2016-06-17 4 views
1

아래의 데이터를 보면 동일한 리프 아래의 상위 수준에있는 ID를 쉽게 찾아서 제거 할 수 있습니다.SQL Server 계층 구조에서 중복 노드 제거

예. ID 4,5는 4, 5, 8, 9 및 12, 13 행에 있습니다. 동일한 ID가 계층 구조 (행 8,9)의 아래쪽에 있으므로 행 4,5를 제거하려고하지만 행 12,13은 별도의 리프에있는 그대로 유지됩니다.

Rows to be Removed

row ID Path 
1 1 /1/ 
2 2 /1/2/ 
3 3 /1/2/3/ 
4 4 /1/2/3/4/ 
5 5 /1/2/3/5/ 
6 6 /1/2/3/6/ 
7 7 /1/2/3/6/7/ 
8 4 /1/2/3/6/4/ 
9 5 /1/2/3/6/5/ 
10 8 /1/2/8/ 
11 7 /1/2/8/7/ 
12 4 /1/2/8/4/ 
13 5 /1/2/8/5/ 
+0

12 및 13은 별도의 리프에 있습니다 (지점을 의미합니까)? 12와 13은 8과 9의 공통 조상을 공유합니다 (즉, 둘 다/1 /와/1/2/모두에서 파생됩니다). 나는 어려워 지려고 노력하지 않고있다. 나는 그 문제를 이해하려고 애 쓰고있다. –

답변

0

예를 들어 최대를위한 ID LEN = 1

select * 
--delete t1 
    from table as t1 
    where exists(
      select * 
       from table as t2 
       where left(t2.path, len(t1.path - 2)) = left(t1.path, len(t1.path - 2)) 
        and charindex(right(t1.path, 2), t2.path, len(t1.path)) > 0 
     ) 
0

이이 ID의 기간 동안 작업을해야 내 접근 방식 :

with 
conversed as (
select substring(reverse(path),CHARINDEX('/',reverse(path),2), len(path)-CHARINDEX('/',reverse(path),2)) inverse_path, id, t.row 
    from t) 
select c.id, c.row keeper, c1.row deletethis 
    from conversed c join conversed c1 
    on c.id = c1.id 
    and c.row <> c1.row 
    and c.inverse_path like '%' +c1.inverse_path; 

OUTPUT

id keeper deletethis 
4 8  4 
5 9  5