Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
oracle Sql中唯一和重复的Sql查询?_Sql_Oracle - Fatal编程技术网

oracle Sql中唯一和重复的Sql查询?

oracle Sql中唯一和重复的Sql查询?,sql,oracle,Sql,Oracle,我需要在Oracle的一列中显示唯一记录,在另一列中显示重复记录 COL1 COL2 1 10 1 10 2 20 3 30 3 30 unique in one set duplicate in one set col1 col2 col1 col2 2 20 1 10

我需要在Oracle的一列中显示唯一记录,在另一列中显示重复记录

COL1 COL2
1     10
1     10
2     20
3     30
3     30

unique in one set             duplicate in one set

col1  col2                    col1 col2
2      20                      1    10
                               1    10
                               3    30
                               3    30

您可以将group by用于having子句的两种情况:

独特记录

select *
from table as t
inner join (
select col1, col2, count(*) as times
from table
group by col1, col2
having count(*) = 1) as t2 ON t.col1 = t2.col2 and t.col2 = t2.col2
重复记录:

select *
from table as t
inner join (
select col1, col2, count(*) as times
from table
group by col1, col2
having count(*) > 1) as t2 ON t.col1 = t2.col1 and t.col2 = t2.col2
select col1, col2
from (select t.*, count(*) over (partition by col1) as cnt
      from t
     ) t
where cnt = 1;

像这样的东西行吗?请参阅代码中的注释

SQL> with
  2  test (col1, col2) as
  3    -- sample data
  4    (select 1, 10 from dual union all
  5     select 1, 10 from dual union all
  6     select 2, 20 from dual union all
  7     select 3, 30 from dual union all
  8     select 3, 30 from dual
  9    ),
 10  uni as
 11    -- unique values
 12    (select col1, col2
 13     from test
 14     group by col1, col2
 15     having count(*) = 1
 16    ),
 17  dup as
 18    -- duplicate values
 19    (select col1, col2
 20     from test
 21     group by col1, col2
 22     having count(*) > 1
 23    )
 24  -- the final result
 25  select u.col1 ucol1,
 26         u.col2 ucol2,
 27         d.col1 dcol1,
 28         d.col2 dcol2
 29  from uni u full outer join dup d on u.col1 = d.col1;

     UCOL1      UCOL2      DCOL1      DCOL2
---------- ---------- ---------- ----------
                               1         10
                               3         30
         2         20

SQL>

可以使用窗口函数识别重复的值,然后过滤每个查询。然后,要获得唯一的记录:

select *
from table as t
inner join (
select col1, col2, count(*) as times
from table
group by col1, col2
having count(*) > 1) as t2 ON t.col1 = t2.col1 and t.col2 = t2.col2
select col1, col2
from (select t.*, count(*) over (partition by col1) as cnt
      from t
     ) t
where cnt = 1;
要获取副本,请执行以下操作:

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

记录不适合列。请向我们展示您当前查询的样本数据。问题不清楚