【发布时间】:2013-12-28 13:54:27
【问题描述】:
我有下表:
| a | b |
|---|---|
| 2 | 4 | x
| 2 | 5 |
| 3 | 1 | x
| 6 | 4 |
| 6 | 5 | x
| 7 | 5 |
| 7 | 4 |
|---|---|
我想选择尽可能多的唯一对,其中 a 或 b 都不重复。因此,旁边带有 x 的条目应该是 select 将抓取的内容。任何想法如何做到这一点?
目前我有一些 SQL 会做相反的事情,选择那些不是唯一的并删除它们,但它并没有按照我想要的方式工作。这是我现在拥有的 SQL,但我想我会放弃它并从我上面所说的角度处理它。
delete t
from #temp2 t
where (exists(select * from #temp2
where (b = t.b
and a < t.a))
or exists(select * from #temp2
where a = t.a
and (b < t.b and ) and
(not exists(select * from #temp2
where b = t.b
and a < t.a)
or not exists(select * from #temp2
where a = t.a
and b < t.b))
谢谢!
【问题讨论】:
-
最大匹配的解决方案将完成这项工作。虽然我并不确切知道如何将其转换为 SQL...en.wikipedia.org/wiki/Matching_%28graph_theory%29
-
{
3,1,6,4,7,5} 也是一种解决方案吗?还是{3,1、6,5、7,4}? -
我只会在 .NET 中使用一个 DataReader 和两个 HashSet
标签: sql algorithm tsql duplicates duplicate-removal