I'm trying to get a dataset put together to feed a GIS application and looking for a SQL Guru to help me avoid using a cursor and temp tables since we have about 30 million records to process (I'm using SQL Server 2012). The main points of the dataset consist of a VehicleID, PositionTime, Latitude, Longitude, and Speed. We need to have the records ordered by VehicleID and PositionTime in order to map the Lat/Long points and track movement. If the Speed is greater than or equal to 0.2, then it's an acceptable record. However, if the speed is less than 0.2, then it's considered as not moving and needs special processing. In the example below, for records one and two, I would need to average the lat, long, and speed, and also keep the maximum position time and put that into a single record. The third and fourth records are fine as is. Records five and six would also need to be combined into one record, so vehicle 1 would go from 6 records to 4 records. After the records are "normalized", we then need to get the date difference between records for each vehicle and position time. The same process would start over again for VehicleID 2 (we have about 7,000 vehicles).
VehicleID PositionTime Latitude Longitude Speed 1 11/20/2013 18:09:27 29.54608 -95.04444 0.1 1 11/20/2013 18:47:35 29.54608 -95.04444 0 1 11/20/2013 20:34:45 29.546105 -95.04442 5 1 11/20/2013 20:46:44 29.54607833 -95.04443167 3 1 11/20/2013 21:01:44 29.54606667 -95.04442833 0 1 11/20/2013 21:16:43 29.546095 -95.04443167 0.1 2 11/20/2013 21:31:44 29.54609 -95.04441 5 2 11/20/2013 21:46:44 29.54607667 -95.04443 0