How to convert ms-access last() function to sql server 2008 equivalent query? code is
SELECT
DISTINCT Last([Title].[Number) AS Row_ID
FROM [Title]
HAV
Try to use LAST_VALUE(Title.Number) over (...)
You need to use sub-query with ORDER BY
because sql-server doesn't guaranty order of rows without this clause . See the example.
declare @tbl table(f1 int, f2 varchar(10), d datetime)
insert into @tbl values (1,'1-first','20120917')
insert into @tbl values (1,'1-middle','20120918')
insert into @tbl values (1,'1-last','20120919')
insert into @tbl values (2,'2-just one','20120917')
select f1, (select TOP 1 f2 from @tbl t2 where t2.f1=t1.f1 order by d DESC) Last_f2
from @tbl t1
group by f1
It has no direct equivalent but can generally be achieved by combinations of ORDER BY
and ROW_NUMBER()
, or possibly MAX
- if you provide more context and sample data then it is easier to give a definitive answer
Try this
Select Top 1 Number From
(
Select Number From Title ORDER BY Number DESC
)