I am not sure where went wrong, but it seems the LAST_VALUE function is not returning the desired result. What I am trying to do is as following
Create table #temp(
a varchar(10),
b varchar(10),
c datetime
)
insert into #temp
Values('aa','bbb','2014-10-15 16:39:41.000'),
('aa','bbb','2014-10-16 06:00:04.000')
select a,b,c,
FIRST_VALUE(c) over (partition by a, b order by c asc) as first_date,
LAST_VALUE(c) over (partition by a, b order by c asc) as last_date,
row_number() over (partition by a, b order by c asc) as rn
from #temp
The result I got is as following, which has different last value.
a | b | c | first_date | last_date | rn
aa | bbb | 2014-10-15 16:39:41.000 | 2014-10-15 16:39:41.000 | 2014-10-15 16:39:41.000 | 1
aa | bbb | 2014-10-16 06:00:04.000 | 2014-10-15 16:39:41.000 | 2014-10-16 06:00:04.000 | 2