Row_number函数返回一个唯一的值,当碰到相同数据时,排名按照记录集中记录的顺序依次递增。一般情况下row_number()函数和over连用
1)ROW_NUMBER() OVER(partition by col1 order by col2) 表示根据col1分组,在分组内部根据col2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内是连续且唯一的)
with
q1 as (select 1 a,1 b from dual
union select 1,2 from dual
union select 2,3 from dual
union select 2,4 from dual
union select 3,5 from dual
union select 3,6 from dual
union select 3,7 from dual
union select 3,8 from dual)
select a,b,row_number() over(partition by a order by b) oid
from q1;
–查询按照A分组后的第一个值
with
q1 as (select 1 a,1 b from dual
union select 1,2 from dual
union select 2,3 from dual
union select 2,4 from dual
union select 3,5 from dual
union select 3,6 from dual
union select 3,7 from dual
union select 3,8 from dual),
q2 as (select a,b,row_number() over(partition by a order by b) oid
from q1)
select a,b,oid from q2 where oid=1;
2)row_number() over(order by column asc/desc)先对列column按照升序/降序排列,再为每条记录返回一个序列号
with
q1 as (select 1 a,1 b from dual
union select 1,2 from dual
union select 3,3 from dual
union select 3,4 from dual
union select 3,5 from dual
union select 3,6 from dual
union select 2,7 from dual
union select 2,8 from dual)
select a,b,row_number() over(order by a asc) from q1;
DENSE_RANK
Dense_rank函数返回一个唯一的值,除非当碰到相同数据时,此时所有相同数据的排名都是一样的。
RANK
Rank函数返回一个唯一的值,除非遇到相同的数据时,此时所有相同数据的排名是一样的,同时会在最后一条相同记录和下一条不同记录的排名之间空出排名。
这三个分析函数的综合使用
–构造测试数据
create table user_order(region_id number(4),customer_id number(4),customer_sales number(10));
select * from user_order;
insert into user_order values(5,1,151162);
insert into user_order values(10 ,29, 903383 );
insert into user_order values( 6 , 7, 971585 );
insert into user_order values(10 ,28, 986964 );
insert into user_order values( 9 ,21, 1020541 );
insert into user_order values( 9 ,22, 1036146 );
insert into user_order values( 8 ,16, 1068467 );
insert into user_order values( 6 , 8, 1141638 );
insert into user_order values( 5 , 3, 1161286 );
insert into user_order values( 5 , 5, 1169926 );
insert into user_order values( 8 ,19, 1174421 );
insert into user_order values( 7 ,12, 1182275 );
insert into user_order values( 7 ,11, 1190421 );
insert into user_order values( 6 ,10, 1196748 );
insert into user_order values( 6 , 9, 1208959 );
insert into user_order values(10 ,30,1216858 );
insert into user_order values( 5 ,2 , 1224992);
insert into user_order values( 9,24, 1224992 );
insert into user_order values( 9,23, 1224992 );
insert into user_order values( 8,18, 1253840 );
insert into user_order values( 7 ,15, 1255591 );
insert into user_order values( 7 ,13, 1310434 );
insert into user_order values(10 ,27,1322747 );
insert into user_order values( 8 ,20, 1413722 );
insert into user_order values( 6 , 6, 1788836 );
insert into user_order values(10 ,26,1808949 );
insert into user_order values( 5 , 4, 1878275 );
insert into user_order values( 7 ,14, 1929774 );
insert into user_order values( 8 ,17, 1944281 );
insert into user_order values( 9 ,25, 2232703 );
commit;
–1)按订单总额排名,查询前12位的客户信息
select
customer_id,
total ,
id_row ,
id_rank ,
id_drank
from
(
select
customer_id ,
sum(customer_sales) total ,
row_number() over(order by sum(customer_sales) desc) id_row,
rank() over(order by sum(customer_sales) desc) id_rank ,
dense_rank() over(order by sum(customer_sales) desc) id_drank
from
user_order
group by
customer_id
order by
total desc
)
where
id_drank<=12;
–2)为各个区域的订单总额进行排名
select
region_id ,
customer_id ,
sum(customer_sales) total ,
row_number() over(partition by region_id order by sum(customer_sales) desc) id_row,
rank() over(partition by region_id order by sum(customer_sales) desc) id_rank ,
dense_rank() over(partition by region_id order by sum(customer_sales) desc) id_drank
from
user_order
group by
region_id,
customer_id
order by
region_id;