Oracle - 查找具有不同值的匹配记录

时间:2013-10-17 20:53:11

标签: sql database oracle

假设我的Oracle DB中有以下表格:

Col1:       Col2: ...   Coln:
1           a     ...   1
1           a     ...   1
1           b     ...   1
1           b     ...   1
1           c     ...   1
1           a     ...   1
2           d     ...   1
2           d     ...   1
2           d     ...   1
3           e     ...   1
3           f     ...   1
3           e     ...   1
3           e     ...   1
4           g     ...   1
4           g     ...   1

而且,我想得到的是一个独特的记录列表,其中Col1Col2不同 - 忽略Col2匹配所有Col1的任何时间}。

所以,在这个例子中我想得到结果集:

Col1:       Col2:
1           a    
1           b     
1           c    
3           e    
3           f   

现在,我想出了如何使用对手头问题感觉相当复杂的查询来执行此操作:

With MyData as
(
   SELECT b.Col1, b.Col2, count(b.Col2) over(Partition By b.Col1) as cnt from 
   (
    Select distinct a.Col1, a.Col2 from MyTable a 
   ) b
)

select Col1, Col2
from MyData
where cnt > 1
order by Col1

我想知道什么是更好的方法 - 我没有设法使用GROUP BY& HAVING并且可能认为这可能是使用自联接来完成的...这更像是一种查看/学习新方法以获得更好(也许更有效)查询的结果。

感谢!!!

2 个答案:

答案 0 :(得分:3)

尝试此查询:

SELECT distinct *
FROM table1 t1
WHERE EXISTS
( SELECT 1 FROM table1 t2
  WHERE t1.col2 <> t2.col2
    AND t1.col1 = t2.col1
) 
order by 1,2

演示:http://www.sqlfiddle.com/#!4/9ce10/12

-----编辑-------

是的,还有其他方法可以做到这一点:

SELECT distinct col1, col2
FROM table1 t1
WHERE col2 <> ANY (
  SELECT col2 FROM table1 t2
  WHERE t1.col1 = t2.col1
) 
order by 1,2;

SELECT distinct col1, col2
FROM table1 t1
WHERE NOT col2 = ALL (
  SELECT col2 FROM table1 t2
  WHERE t1.col1 = t2.col1
) 
order by 1,2
;

SELECT distinct t1.col1, t1.col2
FROM table1 t1
JOIN table1 t2
ON t1.col1 = t2.col1 AND t1.col2 <> t2.col2 
order by 1, 2
;


SELECT t1.col1, t1.col2
FROM table1 t1
JOIN table1 t2
ON t1.col1 = t2.col1 
GROUP BY t1.col1, t1.col2
HAVING COUNT( distinct t2.col2 ) > 1
order by 1, 2
;


SELECT t1.col1, t1.col2
FROM 
table1 t1
JOIN (
  SELECT col1
  FROM table1
  GROUP BY col1
  HAVING COUNT( distinct col2 ) > 1
) t2
ON t1.col1 = t2.col1
GROUP BY t1.col1, t1.col2
ORDER BY t1.col1, t1.col2
;

演示 - &gt; http://www.sqlfiddle.com/#!4/9ce10/33

试试这些,我真的不知道他们将如何处理您的数据 但是,创建一个复合索引:

CREATE INDEX name ON table1( col1, col2 )

很可能会加速所有这些查询。

答案 1 :(得分:1)

以下是使用聚合和分析函数的方法:

with t as (
      select col1, col2,
             count(*) over (partition by col1) as cnt
      from table1
      group by col1, col2
     )
select col1, col2
from t
where cnt > 1;

我想做的是:

  select col1, col2,
         count(*) over (partition by col1) as cnt
  from table1
  group by col1, col2
  having count(*) over (partition by col1) > 1;

但是,这不是有效的SQL,因为having子句中不允许使用分析函数。