Oracle Analytic Functions

analytic_function::=


analytic_clause::= 

 

query_partition_clause::=

 

order_by_clause::=

 

windowing_clause ::=

 

1、创建表

create table LIB
(
  BILL_MONTH VARCHAR2(10),
  AREA_CODE  VARCHAR2(8),
  NET_TYPE   CHAR(1),
  LOCAL_FARE NUMBER(10,2)
)

2、插入数据

insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200405', '5761', 'G', 7393344.04);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200405', '5761', 'J', 5667089.85);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200405', '5762', 'G', 6315075.96);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200405', '5762', 'J', 6328716.15);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200405', '5763', 'G', 8861742.59);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200405', '5763', 'J', 7788036.32);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200405', '5764', 'G', 6028670.45);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200405', '5764', 'J', 6459121.49);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200405', '5765', 'G', 13156065.77);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200405', '5765', 'J', 11901671.7);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200406', '5761', 'G', 7614587.96);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200406', '5761', 'J', 5704343.05);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200406', '5762', 'G', 6556992.6);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200406', '5762', 'J', 6238068.05);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200406', '5763', 'G', 9130055.46);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200406', '5763', 'J', 7990460.25);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200406', '5764', 'G', 6387706.01);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200406', '5764', 'J', 6907481.66);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200406', '5765', 'G', 13562968.81);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200406', '5765', 'J', 12495492.5);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200407', '5761', 'G', 7987050.65);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200407', '5761', 'J', 5723215.28);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200407', '5762', 'G', 6833096.68);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200407', '5762', 'J', 6391201.44);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200407', '5763', 'G', 9410815.91);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200407', '5763', 'J', 8076677.41);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200407', '5764', 'G', 6456433.23);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200407', '5764', 'J', 6987660.53);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200407', '5765', 'G', 14000101.2);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200407', '5765', 'J', 12301780.2);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200408', '5761', 'G', 8085170.84);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200408', '5761', 'J', 6050611.37);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200408', '5762', 'G', 6854584.22);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200408', '5762', 'J', 6521884.5);
insert into LIB (BILL_MONTH, AREA_CODE, NET_TYPE, LOCAL_FARE)
values ('200408', '5763', 'G', 9468707.65);
commit;

 

3、测试

select lib.area_code,
       sum(lib.local_fare) sum_fare,
       rank() over(order by sum(lib.local_fare) desc) fare_rank
  from lib
 group by lib.area_code
------------
select lib.area_code,
       sum(lib.local_fare) sum_fare,
       dense_rank() over(order by sum(lib.local_fare) desc) fare_rank
  from lib
 group by lib.area_code
--------
select lib.area_code,
       sum(lib.local_fare) sum_fare,
       row_number() over(order by sum(lib.local_fare) desc) fare_rank
  from lib
 group by lib.area_code
--------
select lib.area_code,
       lib.bill_month,
       lib.local_fare,
       lag(lib.local_fare, 2, 0) over(partition by lib.area_code order by lib.bill_month) pre_local_fare,
       lag(lib.local_fare, 1, 0) over(partition by lib.area_code order by lib.bill_month) last_local_fare,
       lead(lib.local_fare, 1, 0) over(partition by lib.area_code order by lib.bill_month) next_local_fare,
       lead(lib.local_fare, 2, 0) over(partition by lib.area_code order by lib.bill_month) post_local_fare
  from (select lib.area_code, lib.bill_month, sum(lib.local_fare) local_fare
          from lib
         group by lib.area_code, lib.bill_month) lib
-----------
select lib.area_code,
       lib.bill_month,
       lib.local_fare,
       sum(lib.local_fare) over(partition by area_code order by to_number(bill_month) range between 1 preceding and 1 following) "3month_sum",
       avg(lib.local_fare) over(partition by area_code order by to_number(bill_month) range between 1 preceding and 1 following) "3month_avg",
       max(lib.local_fare) over(partition by area_code order by to_number(bill_month) range between 1 preceding and 1 following) "3month_max",
       min(lib.local_fare) over(partition by area_code order by to_number(bill_month) range between 1 preceding and 1 following) "3month_min"
  from (select lib.area_code, lib.bill_month, sum(lib.local_fare) local_fare
          from lib
         group by lib.area_code, lib.bill_month) lib
------------
select lib.bill_month,
       lib.area_code,
       sum(lib.local_fare) local_fare,
       ratio_to_report(sum(lib.local_fare)) over(partition by lib.bill_month) area_pct
  from lib
 group by lib.bill_month, lib.area_code
-------
select lib.bill_month,
       lib.area_code,
       sum(lib.local_fare) local_fare,
       first_value(lib.area_code) over(order by sum(lib.local_fare) desc rows unbounded preceding) firstval,
       first_value(lib.area_code) over(order by sum(lib.local_fare) asc rows unbounded preceding) lastval
  from lib
 group by lib.bill_month, lib.area_code
 order by lib.bill_month

 

你可能感兴趣的:(oracle,.net,J#)