How to retrieve the LAST record from a TOP(5) SQL Select Statement

Posted on 2011-09-27
Last Modified: 2012-05-12
SELECT     TOP (5) CalendarDays
FROM         Calendar
WHERE     (CalendarDays >= '11/15/11') AND (OfficeWorkdays <> 'Workday')

Returns 5 records of calendar days. How can I retrieve the last record/date from within Delphi? I need to assign a variable to that last record...

Question by:Bianca
  • 2

Expert Comment

ID: 36712035
Can you please give some more detail? I can not understand your requirement.

Accepted Solution

mmr159 earned 500 total points
ID: 36712039
SELECT MAX(CalendarDays)
    SELECT     TOP (5) CalendarDays
    FROM         Calendar
    WHERE     (CalendarDays >= '11/15/11') AND (OfficeWorkdays <> 'Workday')
    ) AS t

Expert Comment

ID: 36712046
OR... you might want to use MIN... depending on what you mean by "Last" record.

Author Comment

ID: 36712050
SELECT     MAX(CalendarDays) AS CalendarDays
FROM         (SELECT     TOP (5) CalendarDays
                       FROM          Calendar
                       WHERE      (CalendarDays >= '11/15/11') AND (OfficeWorkdays = 'Workday')) AS Calendar_1

I got it a minute before seeing this post, thanks for your help!!!

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi IDE crash without error message ... 7 76
Passing value to a stored procedure 8 91
Grid querry results 41 72
SQL Restore Script - Syntax Error 8 91
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now