SQL SERVER TUTORIAL IN DEPTH

The blog is to help users about sql server

Others

LightBlog

Breaking

Saturday, 4 January 2014

First_Value, Last_Value and Lead function in Sql Server 2012

create table #temp1
(
userid int,
username varchar(50)
)


insert into #temp1
select 12,'siba'
union all select 13,'anurag'
union all select 13,'abhishek'
union all select 14,'ravi'
union all select 14,'anurag'
union all select 15,'siba'
union all select 19,'siba'
select 12,'tom'

select userid,username,first_value(userid) over(order by userid desc) as firstvalue,last_value(userid) over(order by userid desc) as lastvalue

 from #temp1
















-------------------------------------------------------------------------
select userid,username,first_value(userid) over(order by userid desc) as firstvalue,last_value(userid)  over(order by userid desc) as lastvalue,
lead(userid) over(order by userid asc) as leaduserid
from #temp1

















-->Last_value is actually the row currently which we are looking at and Lead is the subsequent row value




No comments:

Post a Comment

 test