Question : Return max or min from a subset of data

I have the following set of Data:

Order     Segment    Date/Time                 StopType
1                 1           1/1/2010 3:35 am      Pickup
1                 1           1/1/2010 5:00 am      Pickup
1                 1           1/2/2010 7:00 am      Delivery
1                 2           1/2/2010 8:00 am      Pickup
1                 2           1/2/2010 9:00 am      Delivery
1                 2           1/2/2010 9:30 am      Delivery
2                 1           1/2/2010 8:00 am      Pickup
2                 1           1/2/2010 9:00 am      Delivery
2                 1           1/2/2010 9:30 am      Delivery

I would like to select, from each segment, the earliest pickup and latest delivery for each order and/or segment, such that the resulting set would be as follows:

Order     Segment    Date/Time                 StopType
1                 1           1/1/2010 3:35 am      Pickup
1                 1           1/2/2010 7:00 am      Delivery
1                 2           1/2/2010 8:00 am      Pickup
1                 2           1/2/2010 9:30 am      Delivery
2                 1           1/2/2010 8:00 am      Pickup
2                 1           1/2/2010 9:30 am      Delivery

The table consists of many orders, I only included two here for simplicity.  I'm not sure how to select the min/max from within a subset of the main set of data (i.e. some I have to select the min/max from each segment, all belonging to one order, and others I need to select the min/max from one segement, all on the same order).

I'm using T-SQL on SQL Server 2008.

Thanks!

Answer : Return max or min from a subset of data

There are many SQL Server operations that use tempdb to perform sorts and things like that.  Tempdb may bloat to very large sizes.  Restarting SQL Server will shrink tempdb since it's recreated every time SQL starts.  However, this may not be an option in your environment.

Best practice is to put tempdb on a separate disk and make sure it's adequately sized for your day to day operations.

FWIW, temp tables should be automatically destroyed when the connection that created them is dropped.  Table variables are good if you have the memory to support them.
Random Solutions  
 
programming4us programming4us