0

我正在使用oracle 10g EE数据库。我有一个表 mytable,有两列,数据如下:

注意:我只想在第二列中查找基于相同值的数据,第一列中是否存在相同或不同的值都没有关系。

10A, B and C重复3 次these 3 are required output

同样20重复 2 次 for CandD这些也是必需的输出

   column1             column2
--------------     ---------------

     A                  10 //required
     A                  10 //required        
     B                  10 //required
     C                  20//required
     D                  20//required
     E                  30--------not required as 30 is only here and not duplicated
     F                  40--------not required as 40 is only here and not duplicated

需要以下输出,即第二列中的相同值在第一列中具有相同或不同的值

   column1             column2
--------------     ---------------

     A                  10         
     A                  10           
     B                  10
     C                  20
     D                  20
4

3 回答 3

3

听起来你想要

SELECT *
  FROM table_name t1
 WHERE column2 IN( SELECT column2
                     FROM table_name t2
                    GROUP BY column2
                   HAVING COUNT(*) > 1 )

这似乎适用于您的示例数据

SQL> with table_name as (
  2    select 'A' column1, 10 column2 from dual union all
  3    select 'A', 10 from dual union all
  4    select 'B', 10 from dual union all
  5    select 'C', 20 from dual union all
  6    select 'D', 30 from dual)
  7  SELECT *
  8    FROM table_name t1
  9   WHERE column2 IN( SELECT column2
 10                       FROM table_name t2
 11                      GROUP BY column2
 12                     HAVING COUNT(*) > 1 );

C    COLUMN2
- ----------
B         10
A         10
A         10
于 2012-04-18T09:55:14.230 回答
3
SELECT column1,
       column2
  FROM <table> t1
 WHERE column2 IN (SELECT column2
                     FROM <table> t2
                    GROUP BY column2
                    HAVING count(*) > 1);
于 2012-04-18T09:55:59.247 回答
0

select * from table where column2 in ( select column2 from table group by coulmn2 with count(*)>1);

应该为你工作。

谢谢阿比

于 2012-04-18T10:08:48.297 回答