Oracle lag and lead analysis functions

Source: Internet
Author: User

The lag and lead analysis functions can retrieve the data of the first n rows of the same field (Lag) and the data of the last n rows (LEAD) as independent columns in the same query.

This operation can replace table self-join, and lag and lead have higher efficiency.

Syntax:

/* Syntax */lag (exp_str, offset, defval) over () lead (exp_str, offset, defval) over () -- exp_str: column to be retrieved -- the number of rows of data after offset -- defval: no default value that meets the condition

1.

/* Basic Data */select T. * From test1 T;

2.

/* Use lag and lead to analyze future data */select T. ID, lag (T. name, 1, 0) over (order by id desc) max_v, T. name, lead (T. name, 1, 0) over (order by id desc) min_vfrom test1 T;

3.

/* Data that meets the query results */select I. * From (select T. ID, lag (T. name, 1, 0) over (order by id desc) max_v, T. name, lead (T. name, 1, 0) over (order by id desc) min_v from test1 t) I where I. name = '3a ';

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.