convert ms-access last() function to sql server 2008

后端 未结 4 408
日久生厌
日久生厌 2020-12-22 12:10

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         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-22 12:55

    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
    

提交回复
热议问题