oracle lag使用情景,lag函数用法
摘要:
下文講述sqlserver 2012中 lag、lead分析函數(shù)的用法及簡(jiǎn)介
實(shí)驗(yàn)環(huán)境: sqlserver 2012
lag、lead:
可以不通過聯(lián)結(jié)的形式獲取上下相鄰(指定偏移量)行的指定列的數(shù)據(jù)
lag 向前指定數(shù)據(jù)
lead向后指定數(shù)據(jù)
——————–
lag、lead參數(shù)說明:
參數(shù)1:列名
參數(shù)2:偏移量offset
參數(shù)3:進(jìn)行偏移量計(jì)算后,無法得到結(jié)果時(shí)的缺省值
例:
lag分析函數(shù)舉例說明
declare @test table(keyId int,info varchar(20),qty int)
insert into @test (keyId,info,qty)values(-1,'a',2)
insert into @test (keyId,info,qty)values(1,'a',2)
insert into @test (keyId,info,qty)values(2,'a',10)
insert into @test (keyId,info,qty)values(3,'b',8)
insert into @test (keyId,info,qty)values(3,'b',8)
insert into @test (keyId,info,qty)values(5,'d',8)
insert into @test (keyId,info,qty)values(6,'b',9)
/*
例: lag 在info群組中,
向前偏移一行, 無法找到值時(shí),使用默認(rèn)值"-9"
keyId=-1 在info ='a'群組中,向前偏移一行,無數(shù)據(jù),所以為默認(rèn)值-9
keyId=1 在info ='a'群組中,向前偏移一行,上一行數(shù)據(jù)為-1 所以顯示-1
keyId=2 在info ='a'群組中,向前偏移一行,上一行數(shù)據(jù)為-1 所以顯示-1
keyId=3 在info ='b'群組中,向前偏移一行,無數(shù)據(jù),所以為默認(rèn)值-9
keyId=3 在info ='b'群組中,向前偏移一行,上一行數(shù)據(jù)為-1 所以顯示3
keyId=6 在info ='b'群組中,向前偏移一行,上一行數(shù)據(jù)為-1 所以顯示3
keyId=5 在info ='d'群組中,向前偏移一行,無數(shù)據(jù),所以為默認(rèn)值-9
*/
select *,
lag(keyId,1,'-9') over(partition by info order by keyId) as lagInfo
from @test
mssql_sqlserver_lag詳解舉例-1
—————————————
mssql_sqlserver_lag詳解舉例-2
lead分析函數(shù)舉例說明
lead 向后查找指定列數(shù)據(jù),作為返回值,同lag的唯一區(qū)別為:查找方向不一樣
declare @test table(keyId int identity,info varchar(20),qty int)
insert into @test (info,qty)values('a',2)
insert into @test (info,qty)values('a',2)
insert into @test (info,qty)values('a',10)
insert into @test (info,qty)values('b',8)
insert into @test (info,qty)values('b',8)
insert into @test (info,qty)values('d',8)
insert into @test (info,qty)values('b',9)
select *,
lead(keyId,1,'-9') over(order by keyId) as lagInfo
from @test
mssql_sqlserver_lead詳解舉例-1
總結(jié)
以上是生活随笔為你收集整理的oracle lag使用情景,lag函数用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux下的lds链接脚本二
- 下一篇: 2021年全国职业院校技能大赛(中职组)