【问题标题】:SQL:how to find duplicated combinations in two adjacent columnsSQL:如何在相邻的两列中查找重复的组合
【发布时间】:2013-05-22 17:22:13
【问题描述】:

我有一张表(field1、field 2、field3、field4), 如何仅筛选出在两个相邻列(field3 和 field4)中包含重复组合的行?即->

【问题讨论】:

  • 对不起,刚发完我的文字就被格式化了,失去了最初的看法,希望这会让它更清晰-A
  • 您使用的是哪个数据库?
  • 请定义adjacent columns 在SQL中,行和列中没有定义顺序。

标签: sql


【解决方案1】:

试试这个:

select *
from mytable t
join (
    select field3, field4, count(*) from (
      select field3, field4 from mytable where field3 <= field4
      union all
      select field4, field3 from mytable where field3 > field4) x
    group by field3, field4
    having count(*) > 1) y
  on (t.field3 = y.field3 and t.field4 = y.field4)
    or (t.field3 = y.field4 and t.field4 = y.field3)

union all 内部查询将所有值排成一行,而不会将重复项(如 union 所做的那样)删除到一致的列中 - where 子句确保不会选择两次行。

然后内部查询与having 子句组合在一起以挑选重复项。

外部查询连接到这两种方式以获取所有行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 2013-04-17
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 2023-02-02
    相关资源
    最近更新 更多