将ms-access last()函数转换为sql server 2008

时间:2022-07-23 15:43:14

How to convert ms-access last() function to sql server 2008 equivalent query? code is

如何将ms-access last()函数转换为sql server 2008等效查询?代码是

SELECT 
    DISTINCT Last([Title].[Number) AS Row_ID 
FROM [Title] 
HAVING (Last([Title].[Number]) Is Null)

4 个解决方案

#1


2  

Try this

尝试这个

Select Top 1 Number From
(
  Select Number From Title ORDER BY Number DESC
)

#2


2  

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

它没有直接的等价物,但通常可以通过ORDER BY和ROW_NUMBER()的组合来实现,或者可能是MAX - 如果你提供更多的上下文和样本数据,那么更容易给出明确的答案

#3


1  

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

尝试使用LAST_VALUE(Title.Number)(...)

#4


0  

You need to use sub-query with ORDER BY because sql-server doesn't guaranty order of rows without this clause . See the example.

您需要对ORDER BY使用子查询,因为sql-server不保证没有此子句的行的顺序。查看示例。

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

#1


2  

Try this

尝试这个

Select Top 1 Number From
(
  Select Number From Title ORDER BY Number DESC
)

#2


2  

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

它没有直接的等价物,但通常可以通过ORDER BY和ROW_NUMBER()的组合来实现,或者可能是MAX - 如果你提供更多的上下文和样本数据,那么更容易给出明确的答案

#3


1  

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

尝试使用LAST_VALUE(Title.Number)(...)

#4


0  

You need to use sub-query with ORDER BY because sql-server doesn't guaranty order of rows without this clause . See the example.

您需要对ORDER BY使用子查询,因为sql-server不保证没有此子句的行的顺序。查看示例。

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