2017-02-09 27 views
-1

重复的结果我已经有通过查询SQL中产生两列如下表:如何删除SQL

Lookup Value Result 
1    2 
2    1 
4    3 
3    4 

正如你可以看到它包含重复的结果。我只想让它显示第一行和第三行。有谁知道如何在SQL中做到这一点?

谢谢

+0

和你有什么试过吗? –

+0

可能的重复[如何删除重复的行?](http://stackoverflow.com/questions/18932/how-can-i-remove-duplicate-rows) –

+8

有没有重复那里... – jarlh

回答

2

有几种方法。下面是一个使用union all

select t.* 
from t 
where col1 < col2 
union all 
select t1.* 
from t1 
where col1 > col2 and 
     not exists (select 1 from t t2 where t1.col1 = t2.col2 and t1.col2 = t2.col1); 

如果你总是知道存在两对(作为示例数据),你可以使用:

select t.* 
from t 
where col1 < col2; 
1
SELECT DISTINCT 
     CASE WHEN Lookup Value < Result 
      THEN Lookup Value 
      ELSE Result 
     END as first, 
     CASE WHEN Lookup Value < Result 
      THEN Result 
      ELSE Lookup Value 
     END as second 
FROM YourTable 
+0

胡安..这可能会产生不在表格中的行,当没有对称重复时。例如:(6,4)可能会出现为(4,6) –

+1

@vkp,您是完全正确的。但欧普说,没有特别的理由,他希望'(1,2)'超过'(2,1)',而'(4,3)'超过'(3,4)''。所以我的猜测是元组顺序无关紧要,就像多米诺骨牌。 –

0
Create Table T (
[Lookup Value] int, 
Result int 
) 

Insert into T values (1,2),(2,1),(4,3),(3,4) 

Select distinct T.[Lookup Value], T.Result 
From T 
where T.[Lookup Value]<=T.Result