【问题标题】:Select greatest number of unique pairs from table从表中选择最大数量的唯一对
【发布时间】: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,16,57,4}?
  • 我只会在 .NET 中使用一个 DataReader 和两个 HashSet

标签: sql algorithm tsql duplicates duplicate-removal


【解决方案1】:

我在这里假设非唯一和唯一是相互排斥的,并且将包含表中的所有记录。如果是这样,请使用您现有的脚本,将其写入 CTE,然后从源表中选择不在 CTE 中的记录加入 CTE。

With Non_Unique_Records as (
--Insert your existing script here
)

Select t.a
    , t.b

From #temp2 t
    Left Outer Join Non_Unique_Records CTE
        on t.a = CTE.a
            and t.b = CTE.b

Where CTE.b is null

然后只需删除 Select 语句返回的记录。

【讨论】:

  • “在此处插入现有脚本”是什么意思?您是指将创建@temp2 表的代码吗?
  • 在你的问题中,你说“目前我有一些 SQL 会做相反的事情,选择那些不是唯一的”。在“--insert your existing script here”的地方插入正确选择那些不唯一的 SQL 脚本
  • 啊,不幸的是,我拥有的用于选择非唯一代码的代码无法正常工作。这就是为什么我想从另一种方式来处理它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 2017-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-26
  • 1970-01-01
相关资源
最近更新 更多