我已经打了一段时间了
我有一个表格,其中包含我使用以下查询创建的重复位置;
select * from Table A where location in
(select location from Table A
group by location having count(*) >1);
重复的位置应该具有与之关联的相同代码。如果复制的位置与其他位置的代码不同,那么我想选择它们是哪些
我有这个示例表
Table A
code location
111 x12345
111 x12345
222 y12346
222 y12346
111 y12346
222 z12348
333 z12348
333 r12340
333 r12340
我想选择具有不同代码的所有重复位置。例如,输出应该如下所示
code location
222 y12346
222 y12346
111 y12346
222 z12348
333 z12348
答案 0 :(得分:0)
您可以使用an analytic count()
获取每个位置使用的代码数量:
select code, location,
count(distinct code) over (partition by location) as cnt
from TableA
然后将其用作子查询,过滤该计数为> 1:
select code, location
from (
select code, location,
count(distinct code) over (partition by location) as cnt
from TableA
)
where cnt > 1;
CODE LOCATION
---------- --------
111 y12346
222 y12346
222 y12346
222 z12348
333 z12348
答案 1 :(得分:0)
另一种(虽然不是更好)选项,没有分析:
SQL> with tableA (code, location) as
2 (select 111, 'x12345' from dual union
3 select 111, 'x12345' from dual union
4 select 222, 'y12346' from dual union
5 select 222, 'y12346' from dual union
6 select 111, 'y12346' from dual union
7 select 222, 'z12348' from dual union
8 select 333, 'z12348' from dual union
9 select 333, 'r12340' from dual union
10 select 333, 'r12340' from dual
11 )
12 select code, location
13 from tableA
14 where location in (select location from tableA
15 group by location
16 having count(distinct code) > 1);
CODE LOCATI
---------- ------
333 z12348
222 z12348
222 y12346
111 y12346
SQL>