oracle grouping sort

Oracle analytical functions are very powerful. As long as we master these methods, a more direct statement is that we can accomplish a lot of work by knowing the functions of these analytical functions.

These functions and simple applications are posted below.

Among them, I want to sit down and explain to the lag() and lead() functions: lag() itself means to delay the number of a certain column, and lead() has the meaning of leading and leading, which means to advance a few lines Display a column of data

RANK()
dense_rank()
[Syntax] RANK ( ) OVER ( [query_partition_clause] order_by_clause )
dense_RANK ( ) OVER ( [query_partition_clause] order_by_clause )

[Function] The main function of the aggregation functions RANK and dense_rank is to calculate a set of values sort value.
[Parameter] The usage of dense_rank is equivalent to rank(),
[Difference] When the density_rank is in a parallel relationship, the relevant level will not be skipped. rank skips
rank(), which is a jump sort. When there are two second places, the next is the fourth place (also in each group).
dense_rank() l is a continuous sorting. When there are two second places, it is still followed by the first place. three.
[Description] Oracle analytic function


[Example]
The main function of the aggregate functions RANK and dense_rank is to calculate the ranking value in a set of values.
  
  Before version 9i, only the analytic function (analytic), that is, calculating the order value of each row from a query result, was based on the field specified by value_exprs in the order_by_clause clause.
  
  Its syntax is:
  
  RANK ( ) OVER ( [query_partition_clause] order_by_clause )
  
  In version 9i, a new aggregate function (aggregate) is added, that is, for a given parameter value, the sorting value is calculated in the set sorting query. These parameters must be constants or constant-value expressions, and must be exactly the same as the number, position, and type of fields in the ORDER BY clause.
  
  Its syntax is:
  
  RANK ( expr [, expr]... ) WITHIN GROUP
  ( ORDER BY
  expr [ DESC | ASC ] [NULLS { FIRST | LAST }]
  [, expr [ DESC | ASC ] [NULLS { FIRST | LAST }] ]...
  )
  
  Example 1:
  
  There is a table Table content is as follows
  
  COL1 COL2
    1 1
    2 1
    3 2
    3 1
    4 1
    4 2
    5 2
    5 2
    6 2
  
  Analysis function: List the Col2 groups and sort them according to Col1, and generate a numeric column. It is more practical to find out the information of the top students in each subject in the score table.
  
  SELECT a.*,RANK() OVER(PARTITION BY col2 ORDER BY col1) "Rank" FROM table a;
  
  结果如下:
  
  COL1 COL2 Rank
    1 1   1
    2 1   2
    3 1   3
    4 1   4
    3 2   1
    4 2   2
    5 2   3
    5 2   3
    6 2   5
  
  例子2:
  
  TABLE:A (科目,分数)
  
  数学,80
  语文,70
  数学,90
  数学,60
  数学,100
  语文,88
  语文,65
  语文,77
  
  现在我想要的结果是:(即想要每门科目的前3名的分数)
    数学,100
  数学,90
  数学,80
  语文,88
  语文,77
  语文,70
  
  那么语句就这么写:
  
  select * from (select rank() over(partition by subject order by score desc) rk,a.* from a) t
  where t.rk<=3;
  
  Example 3:
  
  Aggregate function: Calculate the value (4,1) in Orade By Col1, Col2 sorting value, that is, col1=4, col2=1 in the sorted position
  
  SELECT RANK(4,3) WITHIN GROUP (ORDER BY col1,col2) "Rank" FROM table; the
  
  result is as follows:
  Rank
  4
  
  dense_rank is equivalent to rank() usage, but there is one difference: in the parallel relationship of dense_rank, the relevant ranks will not be skipped. rank is skipped
  
  For example : table
  
  A B C
  a liu wang
  a jin shu
  a cai kai
  b yang du
  b lin ying
  b yao cai
  b yang 99
  
  For example: when rank is:
  
  select m.a,m.b,m.c,rank() over(partition by a order by b) liu from test3 m
  
   A     B       C     LIU
   a     cai      kai     1
   a     jin      shu     2
   a     liu      wang     3
   b     lin      ying     1
   b     yang     du      2
   b     yang     99      2
   b     yao      cai     4
  
  而如果用dense_rank时为:
  
  select m.a,m.b,m.c,dense_rank() over(partition by a order by b) liu from test3 m
  
   A     B       C     LIU
   a     cai     kai     1
   a     jin     shu     2
   a     liu     wang     3
   b     lin     ying     1
   b     yang     du      2
   b     yang     99      2
   b yao cai 3 ROW_NUMBER()
[Syntax] ROW_NUMBER() OVER (PARTITION BY COL1 ORDER BY COL2)
[Function] Indicates grouping according to COL1, and sorting according to COL2 within the group, and this value indicates the order number after the internal sorting of each group (Consecutive and unique in the group)
row_number() mainly returns "row" information, and does not have a ranking
[parameter]
[Description] Oracle analysis function

Main function: used to take the first few, or the last few, etc.

[Example ]
The table content is as follows:
name | seqno | description
A | 1 | test
A | 2 | test
A | 3 | test
A | 4 | test
B | 1 | test
B | 2 | test
B | 3 | test
B | 4 | test
C | 1 | test
C | 2 | test
C | 3 | test
C | 4 | test

I want to have a sql statement, the result of the search is
A | 1 | test
A | 2 | test
B | 1 | test
B | 2 | test
C | 1 | test
C | 2 | test
implementation:
select name,seqno,description
from(select name,seqno,description,row_number() over (partition by name order by seqno) id
from table_name) where id <=3;
lag() and lead()
[Syntax]
lag(EXPR,<OFFSET>,<DEFAULT>)
LEAD(EXPR,<OFFSET>,<DEFAULT>)
[Function] Indicates grouping according to COL1, within the grouping according to COL2 is sorted, and this value represents the sequence number of each group after sorting (consecutive and unique in the group)
lead () next value lag () previous value

[parameter]
EXPR is the expression returned from other rows
OFFSET is Defaults to a positive number of 1, indicating the relative number of lines. The offset
DEFAULT of the current row partition desired to retrieve is the value returned when the number represented by OFFSET exceeds the range of the grouping.
[Description] Oracle analytic function


[Example]
-- Create table
create table LEAD_TABLE
(
CASEID VARCHAR2(10),
STEPID VARCHAR2(10),
ACTIONDATE DATE
)
tablespace COLM_DATA
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);

insert into LEAD_TABLE values('Case1','Step1',to_date('20070101','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case1','Step2',to_date('20070102','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case1','Step3',to_date('20070103','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case1','Step4',to_date('20070104','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case1','Step5',to_date('20070105','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case1','Step4',to_date('20070106','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case1','Step6',to_date('20070101','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case1','Step1',to_date('20070201','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case2','Step2',to_date('20070202','yyyy-mm-dd'));
insert into LEAD_TABLE values('Case2','Step3',to_date('20070203','yyyy-mm-dd'));
commit; Case1 Step5 2007-1-5 Step4 2007-1-6 Step4 2007-1-4 Case1 Step4 2007-1-4 Step5 2007-1-5 Step3 2007-1-3 Case1 Step3 2007-1-3 Step4 2007-1-4 Step2 2007 -1-2 Case1 Step2 2007-1-2 Step3 2007-1-3 Step1 2007-1-1 Case1 Step1 2007-1-1 Step2 2007-1-2




The results are as follows:






Case1 Step4 2007-1-6 Step6 2007-1-7 Step5 2007-1-5
Case1 Step6 2007-1-7 Step4 2007-1-6
Case2 Step1 2007-2-1 Step2 2007-2-2
Case2 Step2 2007-2-2 Step3 2007-2-3 Step1 2007-2-1
Case2 Step3 2007-2-3 Step2 2007-2-2

还可以进一步统计一下两者的相差天数

select caseid,stepid,actiondate,nextactiondate,nextactiondate-actiondate datebetween from (
select caseid,stepid,actiondate,lead(stepid) over (partition by caseid order by actiondate) nextstepid,
lead(actiondate) over (partition by caseid order by actiondate) nextactiondate,
lag(stepid) over (partition by caseid order by actiondate) prestepid,
lag(actiondate) over (partition by caseid order by actiondate) preactiondate
from lead_table)
结果如下:

Case1 Step1 2007-1-1 2007-1-2 1
Case1 Step2 2007-1-2 2007-1-3 1
Case1 Step3 2007-1-3 2007-1-4 1
Case1 Step4 2007-1-4 2007-1- 5 1
Case1 Step5 2007-1-5 2007-1-6 1
Case1 Step4 2007-1-6 2007-1-7 1
Case1 Step6 2007-1-7
Case2 Step1 2007-2-1 2007-2-2 1
Case2 Step2 2007-2-2 2007-2-3 1
Case2 Step3 2007-2-3


Each record can be connected to the content of the previous/next row

lead () next value lag () previous value

select caseid, stepid, actiondate, lead(stepid) over (partition by caseid order by actiondate) nextstepid,
lead(actiondate) over (partition by caseid order by actiondate) nextactiondate,
lag(stepid) over (partition by caseid order by actiondate) prestepid,
lag(actiondate) over (partition by caseid order by actiondate) preactiondate
from lead_table

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326940310&siteId=291194637
Recommended