Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Min Time of Record

Author  Topic 

MichelleMabbs
Starting Member

24 Posts

Posted - 2015-02-13 : 10:45:14
Hi,

Of a series of records I would like to return the latest time stamp as well as the earliest time stamp out of a series of records.

The key would be the same ID so i need a function or query that can search through each record and find the earliest time for that ID and the latest time.

ID LatestTime EarliestTime
1 05:00:00 02:00:30

I have achieved the latest time by using a RowNumber function and returning the latest record. The stickler is how to find the earliest record as well

Any help appreciated.

Michelle

Michelle

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2015-02-13 : 11:04:25
One of these?
SELECT Id,
MAX(TimeColumn) AS LastestTime,
MIN(TimeColumn) AS EarliestTime
FROM
YourTable
GROUP BY
Id;

-- or

DECLARE @id INT = 1;
SELECT Id,
MAX(TimeColumn) AS LastestTime,
MIN(TimeColumn) AS EarliestTime
FROM
YourTable
WHERE
Id = @Id
GROUP BY
Id;
Go to Top of Page

MichelleMabbs
Starting Member

24 Posts

Posted - 2015-02-13 : 11:09:36
Thank you

Michelle
Go to Top of Page
   

- Advertisement -