如何在使用分析函数后将行放在一起

时间:2016-10-11 13:58:48

标签: sql oracle window-functions

create table xyz ( contrno number , mobile number primary key); 

insert into xyz values(1003288127,123456); 
insert into xyz values(1003288127,123457); 
insert into xyz values(1003288127,123458); 
insert into xyz values(1003288127,123459); 
insert into xyz values(1003288127,123450); 
insert into xyz values(1003288127,123451); 
insert into xyz values(1003288127,123452); 
insert into xyz values(1003288127,123453); 
insert into xyz values(1003288127,123454); 
insert into xyz values(1003288127,123455); 

我希望行应按照控制计数的降序排列,并且所有控制行应该在一起,这意味着rownum应该是顺序的, 我写了这个查询

select c.* 
from xyz c 
order by count(c.contrno) over ( partition by c.contrno ) desc) t 

根据控制的计数来排列行,这些行是正确的,但不是所有的控制在一起

但是当我通过以下查询

查询rownum时
select k.* from (select rownum rn ,t.* from(select c.* 
from xyz c 
order by count(c.contrno) over ( partition by c.contrno ) desc) t ) k 
where k.contrno=1003288127 

输出

rn contrno 
1 51024 1003288127 
2 51025 1003288127 
3 51089 1003288127 
4 51090 1003288127 
5 51091 1003288127 
6 51092 1003288127 
7 51093 1003288127 
8 51094 1003288127 
9 51095 1003288127 
10 51096 1003288127 
11 51097 1003288127 

所以在这里,如果你看到51024和51025之后,51089正在开始,在51025和51089之间,其他的控制即将到来。

请回答为什么会发生以及如何编写可以根据顺序rownum提供输出的查询

1 个答案:

答案 0 :(得分:1)

如果我理解正确,你想先通过计数来点击,然后通过contrno:

order by count(c.contrno) over ( partition by c.contrno ) desc, c.contrno

甚至也可以通过移动设备

order by count(c.contrno) over ( partition by c.contrno ) desc, c.contrno,  c.mobile