title: Return only one row when multiple rows exist
body: <p>I am working with a database that tracks field trip information for schools.  The query will run on a MS SQL 2005 Server.  There are a few cases when my query will return multiple rows for the same Field Trip.  So, what I want to do is filter my results so that <strong>if more than one row per TripID is returned, display only the row with the MIN StartDateTime.</strong></p>
  
  <p>I know there's something I can do with the PARTITION and MIN functions but I'm not sure how to go about it.</p>
  
  <p>Here is my code:</p>
  
  <pre><code> SELECT DISTINCT    
          dbo.Trip_TripInformation.RecordID AS TripID,
          dbo.Trip_TripInformation.TripDate,
          Origin.LocationName AS Origin,
          dbo.Trip_TripInformation.OriginDepartureTime AS StartDateTime,
          dbo.Trip_TripInformation.OriginReturnTime AS ReturnDateTime, 
          ISNULL(dbo.Trip_TripInformation.NoOfStudents, 0) AS NumberOfStudents,
          ISNULL(dbo.Trip_TripInformation.NoOfAdults, 0) AS NumberOfAdults,
          ISNULL(dbo.Trip_TripInformation.NoOfStudents, 0) + ISNULL(dbo.Trip_TripInformation.NoOfAdults, 0) AS NumberOfPassengers,
          Destination.LocationName AS Destination,
          dbo.Vehicles.Vehicle,
          Driver.LastName + ', ' + Driver.FirstName AS Driver    
  FROM dbo.Trip_TripInformation
  LEFT JOIN dbo.Trip_Location AS Origin ON Origin.RecordID = dbo.Trip_TripInformation.OriginLocationID    
  LEFT JOIN dbo.Trip_TripDestinations ON dbo.Trip_TripInformation.RecordID = dbo.Trip_TripDestinations.TripID    
  LEFT JOIN dbo.Trip_Location AS Destination ON Destination.RecordID = dbo.Trip_TripDestinations.LocationID    
  LEFT JOIN dbo.Trip_TripDriverVehicle ON dbo.Trip_TripInformation.RecordID = dbo.Trip_TripDriverVehicle.TripID 
                                      AND dbo.Trip_TripDriverVehicle.DestinationID = dbo.Trip_TripDestinations.RecordID    
  LEFT JOIN dbo.Vehicles ON dbo.Vehicles.RecordID = dbo.Trip_TripDriverVehicle.VehicleID    
  LEFT JOIN dbo.Employees AS Driver ON dbo.Trip_TripDriverVehicle.DriverID = Driver.RecordID    
  ORDER BY TripID
  </code></pre>