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.
| Author |
Topic |
|
Jamhin4
Starting Member
4 Posts |
Posted - 2010-02-09 : 20:45:06
|
| Below is my basic starting query and results. What I need to pull is the most current Facility Status, so all I want to pull in the end is the top row, shown in the results. I attempted to use MAX(dbo.tblFacilityStatusHistory.LastUpdated) AS MAXLastUpdatedin the select statement and ended up with more rows than I wanted.Query:SELECT dbo.tblFacilityStatusHistory.FacilityNumber AS Fac#, dbo.refFacilityStatus.FacilityStatusDesc, dbo.tblFacilityStatusHistory.LastUpdatedFROM dbo.tblFacilityStatusHistory INNER JOIN dbo.refFacilityStatus ON dbo.tblFacilityStatusHistory.FacilityStatusID = dbo.refFacilityStatus.FacilityStatusIDWHERE (dbo.tblFacilityStatusHistory.FacilityNumber = '30044')ORDER BY dbo.tblFacilityStatusHistory.LastUpdated DESCResults:Fac# FacilityStatusDesc LastUpdated30044 Regular 01/12/2010 13:5830044 Closed 01/12/2010 13:5830044 Probationary Provisional 01/11/2010 13:1930044 Closed 01/11/2010 13:0330044 New Provisional 01/11/2010 13:0330044 Closed 01/11/2010 10:3130044 New Provisional 01/04/2010 11:1830044 Pending Application 11/18/2009 10:17Can someone please show me how to write my query out so it only pulls that first row??? I've been battling with this thing for hours, trying to make the MAX thing work, but always end up with the max of 01/12, 01/11 and then it also includes 01/04 and 11/18 each time.HUGE THANKS to anyone who can help me!!!Jamie |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-02-09 : 20:46:53
|
SELECT TOP 1 . . . . ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Jamhin4
Starting Member
4 Posts |
Posted - 2010-02-09 : 20:54:25
|
OMG - (slaps head HARD)Well, if I felt stupid by writing the first post, I feel doubly so now! That TOTALLY WORKS!!!You've officially saved my life and maybe my JOB.MEGA THANKS!!!!!Newbert |
 |
|
|
|
|
|