Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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 EarliestTime1 05:00:00 02:00:30I 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 wellAny help appreciated.MichelleMichelle
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 EarliestTimeFROM YourTableGROUP BY Id; -- orDECLARE @id INT = 1;SELECT Id, MAX(TimeColumn) AS LastestTime, MIN(TimeColumn) AS EarliestTimeFROM YourTableWHERE Id = @IdGROUP BY Id;