How to use analysis statistics functions such as lag () and lead () in Oracle (statistics monthly growth rate)

Source: Internet
Author: User

The LAG () and lead () statistics functions are able to fetch the first n rows of the same field and the values of the last n rows in a single query . Such operations can be implemented using table joins to the same table, but using lags and leads is more efficient. The following examples of lag () and lead () are organized:

LAG (expression,<offset>,<default>)
Sql> Select Year,region,profit, Lag (profit,1) over (order by year) as 51xit_exp from test;
Year Region PROFIT 51xit_exp
---- ------- ---------- -------------
2003 West 88
2003 West 88 88
2003 Central 101 88
2003 Central 100 101
2003 East 102 100
2004 West 77 102
2004 East 103 77
2004 West 89 103

Lead (expresion,<offset>,<default>)
Sql> Select Year,region,profit, Lead (profit,1) through (order by year) as Next_year_exp from test;
Year Region PROFIT Next_year_exp
---- ------- ---------- -------------
2003 West 88 88
2003 West 88 101
2003 Central 101 100
2003 Central 100 102
2003 East 102 77
2004 West 77 103
2004 East 103 89
2004 West 89

The lag function is lag (exp,n,defval), and Defval is the value returned if the function has no value available. The lead function is used in a similar way.
The lead and lag functions are also able to use groupings, and the following are examples of using region groupings:
Sql> Select Year,region,profit, Lag (profit,1,0) over (PARTITION by region order by year) as 51xit_exp from test;
Year Region PROFIT 51xit_exp
---- ------- ---------- -------------
2003 Central 101 0
2003 Central 100 101
2003 East 102 0
2004 East 103 102
2003 West 88 0
2003 West 88 88
2004 West 77 88
2004 West 89 77

A SQL question answer:
Problem:
CREATE TABLE ldy_temp_2
(
Branch VARCHAR (255),
Police station VARCHAR (255),
Document Type VARCHAR (255),
ID number VARCHAR (255),
Name VARCHAR (255),
Sex VARCHAR (255),
Administrative divisions VARCHAR (255),
Hotel name VARCHAR (255),
Hotel Address VARCHAR (255),
Room number VARCHAR (255),
Check-in time VARCHAR (255),
col012 VARCHAR (255)
);

INSERT into ldy_temp_2
(ID number, name, hotel name, check-in time)
VALUES (' 1234 ', ' Zhangtao ', ' A ', ' 20100506 ');
INSERT into ldy_temp_2
(ID number, name, hotel name, check-in time)
VALUES (' 1234 ', ' Zhangtao ', ' A ', ' 20100507 ');
INSERT into ldy_temp_2
(ID number, name, hotel name, check-in time)
VALUES (' 1234 ', ' Zhangtao ', ' B ', ' 20100508 ');



INSERT into ldy_temp_2
(ID number, name, hotel name, check-in time)
VALUES (' 1234 ', ' Zhangtao ', ' A ', ' 20100509 ');
INSERT into ldy_temp_2
(ID number, name, hotel name, check-in time)
VALUES (' 1235 ', ' ZZZZ ', ' A ', ' 20100506 ');
INSERT into ldy_temp_2
(ID number, name, hotel name, check-in time)
VALUES (' 1235 ', ' ZZZZ ', ' B ', ' 20100507 ');
INSERT into ldy_temp_2
(ID number, name, hotel name, check-in time)
VALUES (' 1235 ', ' ZZZZ ', ' A ', ' 20100508 ');
INSERT into ldy_temp_2
(ID number, name, hotel name, check-in time)
VALUES (' 1235 ', ' ZZZZ ', ' B ', ' 20100509 ');

The build statement and test data have been given how to find people who have different names for each of the two neighboring hotels; that is, if a person's ID number is 123, then this person's information will not be the same as the hotel name of two adjacent data after the check-in time.

Answer:
With Temp_a as
(SELECT
T. ID number,
T. Hotel Name,
T. Check-in time,
Lag (T. Hotel name) over (partition by T. ID number ORDER by T. Check-in time) as Lagname
From Ldy_temp_2 t)
Select ID number, name, hotel name, check-in time
From Ldy_temp_2 A
where a. Credential number not in (select B. ID number from temp_a b where b. Hotel name =b.lagname)


How to use analysis statistics functions such as lag () and lead () in Oracle (statistics monthly growth rate)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.