2015-09-07 29 views
1

有人可以帮我解决这个问题吗?任何其他选项都没有使用Oracle分析功能

CREATE TABLE TT (
    A NUMBER PRIMARY KEY, 
    B VARCHAR2(5) 
); 

insert into tt values (11,'A'); 
insert into tt values (12,'A'); 
insert into tt values (13,'B'); 
insert into tt values (14,'B'); 
insert into tt values (15,'C'); 
insert into tt values (16,'D'); 
insert into tt values (17,'E'); 
insert into tt values (18,'E'); 
insert into tt values (19,'F'); 
insert into tt values (20,'F'); 

COMMIT; 

SELECT * FROM TT; 

+---+---+ 
| A | B | 
+---+---+ 
|11 | A | 
|12 | A | 
|13 | B | 
|14 | B | 
|15 | C | 
|16 | D | 
|17 | E | 
|18 | E | 
|19 | F | 
|20 | F | 
+---+---+ 

我的要求是什么的“B”列已映射“A”列赞(值“E”已在“A”的柱映射两行) O/P

的多于一个
+---+ 
| A | 
+---+ 
| 11| 
| 12| 
| 13| 
| 14| 
| 17| 
| 18| 
| 19| 
| 20| 
+---+ 

我已经实现了使用下面的分析查询。我想知道是否可以在没有分析功能的情况下进行归档。

select a 
from (SELECT tt.*, COUNT(*) over (partition by b) cnt 
     FROM TT 
    ) 
where cnt >= 2; 

+0

为什么你不想使用分析功能? –

+0

其很好,但我想知道任何其他选项,以达到相同的结果 –

回答

0

您可以使用一组由考生:

select a 
from tt 
where B in (
      select B 
      from tt 
      group by b 
      having count(*) >= 2); 
+0

谢谢弗洛林:) –

1

这是聚合很容易:

select a 
from tt 
where b in (select b from tt group by b having count(*) > 1); 

作为一个说明,你能避免使用的聚合,因为Oracle提供了rowid伪列:

select a 
from tt 
where exists (select 1 
       from tt tt2 
       where tt2.b = tt.b and tt2.rowid <> tt.rowid 
      ); 
+0

谢谢戈登:) –

0
select a from temp a where exists (select null from temp b where a.b = b.b group by b having count(1) >= 2) 

这是替代solution.i认为你不能避免在这种情况下使用聚合函数。

+0

你可以避免聚合。 –

+0

是的Gordon看到了你没有使用聚合函数的解决方案.. :-) –

相关问题