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

后端 未结 4 383
日久生厌
日久生厌 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:45

    Try to use LAST_VALUE(Title.Number) over (...)

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-22 13:01

    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

    0 讨论(0)
  • 2020-12-22 13:04

    Try this

    Select Top 1 Number From
    (
      Select Number From Title ORDER BY Number DESC
    )
    
    0 讨论(0)
提交回复
热议问题