博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL SERVER LEAD AND LAG FUNCTION
阅读量:6819 次
发布时间:2019-06-26

本文共 1382 字,大约阅读时间需要 4 分钟。

 The following explanation from MSDN

LEAD provides access to a row at a given physical offset that follows the current row. Use this analytic function in a SELECT statement to compare values in the current row with values in a following row.

LAG provides access to a row at a given physical offset that comes before the current row. Use this analytic function in a SELECT statement to compare values in the current row with values in a previous row.


 

--A. SELECT Territory, _Year, Profit,LEAD(Profit, 1, 0) OVER (PARTITION BY Territory ORDER BY _Year) AS PrevProfitFROM Profits--B.SELECT Territory, _Year, Profit,LAG(Profit, 1, 0) OVER (PARTITION BY Territory ORDER BY _Year) AS PrevProfitFROM Profits--C.SELECT Territory, _Year, Profit,LAG(Profit, 2, 0) OVER (PARTITION BY Territory ORDER BY _Year) AS PrevProfitFROM Profits

 

First of all, compare with "LEAD" and "LAG"(A and B region code), here is the result:

Compare with 1 and 2 region only, if it's "LAG"(B region code), _Year=2002 correspondent prevProfit=2001's profit and _Year=2001 correspondent prevProfit=2000's profit, but _Year=2000 no correspondent prevProfit, as it's LAG(Profit, 1, 0), so _Year=2000 correspondent prevProfit is 0."LEAD" it's opposite of "LEAD", _Year=2000's prevProfit= 2001's Profit.

 

 2 within as to LAG(Profit, 2, 0), you can reference below code

转载于:https://www.cnblogs.com/ziqiumeng/p/10169041.html

你可能感兴趣的文章
CountDownTimer,0,0
查看>>
mac 终端 常用命令
查看>>
对VM挂载新加入的磁盘
查看>>
MyEclipse *的安装步骤和破解(32位和64位皆适用)(图文详解)
查看>>
如何撤销 PhpStorm/Clion 等 JetBrains 产品的 “Mark as Plain Text” 操作 ?
查看>>
利用RTMFP开发P2P应用
查看>>
使用maven创建web项目
查看>>
第三十八章 springboot+docker(maven)
查看>>
构建单页面应用
查看>>
BZOJ4078 : [Wf2014]Metal Processing Plant
查看>>
变量的数据类型的转换
查看>>
codevs1022 覆盖[Hungary 二分图最大匹配]
查看>>
mybatis同时启用mapperscanner和传统DAO
查看>>
Spring AOP 通过order来指定顺序
查看>>
记一次SQLServer的分页优化兼谈谈使用Row_Number()分页存在的问题
查看>>
Deci and Centi Seconds parsing in java
查看>>
TestNg依赖高级用法之强制依赖与顺序依赖------TestNg依赖详解(二)
查看>>
Javascript中构造函数与new命令
查看>>
java selenium webdriver处理JS操作窗口滚动条
查看>>
C#------数字转中文
查看>>