Oracle analysis functions Lead (), Lag ()

Source: Internet
Author: User

The Oracle analysis functions Lead () and Lag () are actually very well understood. Lead () is to take the next record of the current order, relative to Lag () is to take the last row of records in the current order. It is often necessary to determine the time difference between two records under certain conditions. The use of the Lead () function is the same as that of Lag (), so you only need to describe one function. Syntax structure: lead (value_expr [, offset] [, default]) over ([query_partition_clause] order by Order_by_clause) parameter description: value_expr value expression, usually a field or expression. Value_expr itself does not support analysis functions, that is, lead does not support multi-layer calls. Offset, which should be a familiar mathematical concept or relative offset. The table is used to open the offset row of the current row, if offset is an integer, it indicates the nth row before the order, and if it is a negative number, it indicates the nth row after the order. If this parameter is not provided, the default value is 1. default. If no value is found, what value should be returned, which is similar to nvl (col, value ). If it is not set and cannot be found, the returned Nullover is considered to be within the range of a result set. If the following partition by is null, it is within the range of the current result set. The query_partition_clause partition statement is optional for the result Set Partition statement. If not, it is all partitions. The Order_by_clause sorting statement is required, for example, order by xxx desc/asc. Here we will introduce the Lag () function, which is the data of the fields in N rows. SQL code with tmp as (select '1' id, 'A' name from dual union all select '2' id, 'bb' name from dual) select. *, lag (name, 1) over (order by id desc) lag from tmp

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.