SQL用于过滤具有类似值的表

时间:2012-08-30 07:02:58

标签: sql oracle

我在table1中有这些数据

table1
ColA    ColB    ColC    ColD
  A       B       C       D
  A       B       G       F
  A       B       C       G
  G       B       C       F
  A       B       C       H

我正在尝试创建一个sql语句,以便在ColB中搜索相同的值,然后在ColD中搜索相同的值

table1
ColA    ColB    ColC    ColD
 A       B       G       F
 G       B       C       F

我试过

select * from table1 where ColB = ColB and ColD = ColD.

无论如何我可以使用一个sql语句过滤掉ColB和ColD中出现的类似数据吗?

1 个答案:

答案 0 :(得分:4)

“类似专栏”:

select colB, ColD 
from table1 
group by colB, ColD
having count(*) > 1

具有“相似列”的数据:

select * 
from table1
join (    select colB, ColD 
    from table1 
    group by colB, ColD
    having count(*) > 1
) a on table1.colB = a.colB and table1.colD = a.colD

另一种方法是:

select * from (
    select 
    s.*,
    count(*) over (partition by colB, ColD) as cnt
    from table1 s
)
where cnt > 1
相关问题