last function in sql server

gholamreza rezaie 61 Reputation points
2021-09-13T12:23:16.417+00:00

hello my friends .
i need last fee of our buying.
like this image i write script but my result is
last' is not a recognized built-in function name .

two table that you see in image is my tables that i use them for my main script.

thanks alot for your guiding131606-question-about-last-function.jpg

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,679 questions
SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
2,797 questions
SQL Server Analysis Services
SQL Server Analysis Services
A Microsoft online analytical data engine used in decision support and business analytics, providing the analytical data for business reports and client applications such as Power BI, Excel, Reporting Services reports, and other data visualization tools.
1,243 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
{count} votes

Accepted answer
  1. EchoLiu-MSFT 14,571 Reputation points
    2021-09-14T01:48:30.89+00:00

    Hi @gholamreza rezaie
    131781-image.png

    As Olaf said, the LAST function is not a built-in function of SQL Server,it is only supported in MS Access.In SQL Server, the LAST function can be replaced by the method mentioned by Erland.

    Applied to your code should look like this:

    SELECT TOP 1 Fee FROM i  
    LEFT JOIN r  
    ON r.condition=i.condition  
    ORDER BY [DATE] DESC;  
    

    If you have any question, please feel free to let me know.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.


2 additional answers

Sort by: Most helpful
  1. Olaf Helper 40,736 Reputation points
    2021-09-13T12:51:47.127+00:00

    LAST is not a valid Transact SQL command.

    Please post table design as DDL, some sample data as DML statement and the expected result.

    0 comments No comments

  2. Erland Sommarskog 100.9K Reputation points MVP
    2021-09-13T21:39:53.493+00:00

    Maybe:

    SELECT TOP 1 Fee
    FROM ...
    ORDER BY Date DESC
    

    But this is quite much a guess on my part of what you are looking for.

    0 comments No comments