TSql Lead 和 Lag 函数

Lead 和 Lag 是TSql中的两个分析函数,执行顺序是在select语句之后,用于对查询的结果集进行前移和后移操作。如果窗口函数没有对查询的结果集分区,那么将整个结果集作为一个分区看待;如果窗口函数对查询的结果集分区,那么Lead和Lag就是对分区进行前移和后移操作。

窗口是由OVER 子句定义查询结果集内的窗口或用户指定的行集,其实就是在select的查询结果集中,将符合条件的多个数据行作为一个窗口,一个select的查询结果集可以划分为多个窗口,也可以仅有一个窗口;每一个窗口都独立于其他窗口,能够单独对窗口进行数据操作。

第一部分:Lead 是 前移操作,“LEAD provides access to a row at a given physical offset that follows the current row”,就是说,将数据窗口向前移动,移动的偏移量由Offset参数决定,窗口向前移动之后,超出数据窗口之外的数据由default参数指定。

1,Syntax

LEAD ( scalar_expression [ ,offset ] , [ default ] )

OVER ( [ partition_by_clause ] order_by_clause )

scalar_expression

  The value to be returned based on the specified offset. It is an expression of any type that returns a single (scalar) value. scalar_expression cannot be an analytic function.

offset

  The number of rows forward from the current row from which to obtain a value. If not specified, the default is 1. offset can be a column, subquery, or other expression that evaluates to a positive integer or can be implicitly converted to bigint. offset cannot be a negative value or an analytic function.

default

  The value to return when scalar_expression at offset is NULL. If a default value is not specified, NULL is returned. default can be a column, subquery, or other expression, but it cannot be an analytic function. default must be type-compatible with scalar_expression.

OVER ( [ partition_by_clause ] order_by_clause)

  partition_by_clause divides the result set produced by the FROM clause into partitions to which the function is applied. If not specified, the function treats all rows of the query result set as a single group. order_by_clause determines the order of the data before the function is applied. When partition_by_clause is specified, it determines the order of the data in each partition. The order_by_clause is required.

Return Types

  The data type of the specified scalar_expression. NULL is returned if scalar_expression is nullable or default is set to NULL.

2,示例

select *
from dbo.test
order by code

3,使用 Lead 平移窗口

3.1,over子句指定的窗口是按照code进行分区,并按照code排序,所以code=1的所有数据行是一个窗口,在窗口中Code的值是code=1,1,1。

Lead 函数操作的字段是Code,将窗口中的Code列向前平移一位,窗口之外的数据使用-1来代替。平移之后,窗口是code=1,1,-1

Lead(code,1,-1)over(partition by code order by code)

select lead(code,1,-1)over(partition by code order by code) as leadid,*
from dbo.test

3.2,窗口函数over指定了窗口,按照name进行分区,按照code进行排序,一个分区内的code进过排顺之后,能够保持窗口值。

在 name=c 的窗口中,Code的值经过排序,已经固定为2,3,3,Lead子句的作用是整个窗口向前移动。

Lead(code,1,-1)over(partition by name order by code)

select Lead(code,1,-1)over(partition by name order by code) as leadid,*
from dbo.test

例如:分区之后,在 name=c 的窗口中,code =2,3,3, 该窗口向前平移一位,code=3,3,-1,由于default=-1,故超出窗口的数值使用-1来填充。

第二部分: Lag

1,Syntax

LAG (scalar_expression [,offset] [,default])
    OVER ( [ partition_by_clause ] order_by_clause )

Arguments 的和 Lead 相同

2,用法和Lead相同,只是移动的方向不同,Lag的方向是向后。

时间: 2024-07-31 07:15:57

TSql Lead 和 Lag 函数的相关文章

oracle中lead和lag函数 (转载)

这两个函数,是偏移量函数,其用途是:可以查出同一字段下一个值或上一个值. lead(col_name,num,flag) col_name是列名:num是取向下第几个值:flag是一个标志,也就是如果向下第几个值是空值的话就取flag: 例如lead(login_time,1,null)这个是向下取一个值,如果这个值为空则按空算,当然也可以用其他值替换. lag(col_name,num,flag) 和lead类似,col_name是列名:num是取向上第几个值:flag是一个标志,也就是如果向

LAG函数实现环比

select *,LAG(金额,1,0)OVER(ORDER BY 年月) 环比金额 from( SELECT Top 1000000 年, 季度, 年月 ,SUM(金额本位币) 金额 FROM ( SELECT * FROM [dbo].[T_output] ) cb_view GROUP BY 年, 季度, 年月 order by 年,季度, 年月 ) BB 如图: 参考: https://blog.csdn.net/mango_love/article/details/84067911 S

oracle listagg函数、lag函数、lead函数 实例

Oracle大师Thomas Kyte在他的经典著作中,反复强调过一个实现需求方案选取顺序: “如果你可以使用一句SQL解决的需求,就使用一句SQL:如果不可以,就考虑PL/SQL是否可以:如果PL/SQL实现不了,就考虑Java存储过程是否可以:如果这些都不可能实现,那么就需要考虑你是否真的需要实现这个需求.” 各个关系型DBMS产品都在遵守关系型数据库模型的基本体系架构,遵循通用的SQL国际规范.同时,为了更好地配合自身数据库实现的特征,以及提供更加丰富的功能,各个DBMS纷纷在标准SQL上

SQL SERVER LEAD和LAG使用

示例:获取在48小时之内重复的记录 SELECT * FROM ( SELECT b.* , LAG(b.OperatorTime, 1, b.OperatorTime) OVER ( PARTITION BY b.No ORDER BY b.OperatorTime ) AS BeforTime , LEAD(b.OperatorTime, 1, b.OperatorTime) OVER ( PARTITION BY b.No ORDER BY b.OperatorTime ) AS Next

SQLServer的Lead和Lag实现

在这里不谈2012版本,因为那版本好像有了lead,lag函数,不过没试过.这里主要讲怎么自己实现 1.有表A如下数据 insert into A(id,name) values(1,'张三') insert into A(id,name) values(2,'李四') insert into A(id,name) values(3,'王五') 2.SQL如下 SELECT * FROM (SELECT A.*,row_number() OVER(order by id) r FROM A) k

SQL点滴20—T-SQL中的排名函数

原文:SQL点滴20-T-SQL中的排名函数 提到排名函数我们首先可能想到的是order by,这个是排序,不是排名,排名需要在前面加个名次序号的,order by是没有这个功能的.还可能会想到identity(1,1),它也给了一个序号,但是不能保证给出的序号是连续升序的.除非能够保证所有的Insert语句都能够正确成功地完成,并且没有删除操作,实际的使用中大多数的表都不能保证这样. 好在SQL Server中提供了一些排名函数来辅助实现这些功能.排名函数按照需要的顺序对数据进行排名,并提供一

Oracle Lead(),Lag()

Lead()就是取当前顺序的下一条记录,相对Lag()就是取当前顺序的上一行记录 语法结构: lead(value_expr [,offset][,default]) over([query_partition_clause] order by Order_by_clause) 参数说明: value_expr 值表达式,通常是字段,也可是是表达式.value_expr本身不支持分析函数,也就是lead不支持多层调用. offset 偏移,应该是很熟悉的数学概念了,或者是相对偏移,表格来开当前行

【T-SQL系列】常用函数—聚合函数

聚合函数平均值AVG.标准偏差STDEV.方差VAR.最大值MAX.最小值MIN.合计SUM.次数COUNT.极差值MAX-MIN.变异系数STDEV/AVG*100 什么是统计统计 就是通过样本特性推断总体特性的过程.类似于赌博,有一定的风险.可信度受取样方法.样本大小等因素的影响.统计是科学的 为什么要用标准差?方差和标准差时表示一组数据离散程度的最好指标,是最常用的差异量数.其特点有:1.反应灵敏,每个数据变化都应在方差上体现:2.计算严密:3.容易计算:4.适合代数运算:5.受抽样变动影

TSQL入门(二) - 排名函数

over() 开窗函数 排名函数必须和over()一起使用,所以先了解over(). OVER 子句定义查询结果集内的窗口或用户指定的行集. 然后,开窗函数将计算窗口中每一行的值. 您可以将 OVER 子句与函数一起使用,以便计算各种聚合值,例如移动平均值.累积聚合.运行总计或每组结果的前 N 个结果. select *,COUNT(*) over() '全班人数' from Student select *,COUNT(*) over(partition by Ssex) '性别人数' fro