从图表数据中删除重复/循环

时间:2017-08-21 14:49:50

标签: sql tsql teradata

我有图表数据,其中图表的弧(包括start [S]和end [E])在关系表中存储如下:

IF OBJECT_ID('tempdb..#Test') IS NOT NULL DROP TABLE #Test;

CREATE TABLE #Test
(
    S NVARCHAR(1)   
    ,E NVARCHAR(1)
);

insert into #Test (S, E) values ('a', 'b');
insert into #Test (S, E) values ('b', 'a');
insert into #Test (S, E) values ('a', 'c');

因此图表由以下弧组成:

a -> b
b -> a
a -> c

我想删除重复项/周期:a - > b和b - > a => a - >湾这可能吗?

2 个答案:

答案 0 :(得分:3)

示例

;with cte as (
    Select *
          ,RN = Row_Number() over (Partition By case when S<E then S+E else E+S end order by s,e)
     From  #Test
)
Delete From cte Where RN>1

更新测试

S   E
a   b
a   c

答案 1 :(得分:0)

如果你只是像你的例子那样得到简单的重复:

DELETE
FROM #test AS t1
WHERE EXISTS
 ( SELECT *
   FROM #test AS t2
   WHERE t1.S = t2.E -- there's the same S/E combination, but switched
     AND t1.E = t2.S
     AND t1.S > t2.S -- delete only one of the combinations
 )