NgKH,What are the rules for TripID?Is TripID unique per Customer? Does the TripID have to be in a sequence per Customer?If the answer are Yes and NO then....--DDL (You should have done this for us!!!)Create table trips (Customer_ID char(3), Cost money, Trip_EndDate datetime)Insert trips (Customer_ID, COST, TRIP_ENDDATE)SELECT '001', 200.00, '01/14/1997'UNION ALLSELECT '001', 300.00, '03/06/1997'UNION ALLSELECT '001', 300.00, '09/16/1997'UNION ALLSELECT '001', 300.00, '09/22/1997'UNION ALLSELECT '001', 300.00, '09/26/1997'UNION ALLSELECT '001', 300.00, '09/30/1997'GO--DML--Create NewTrips tableSelect *, CASE (Select COUNT(*) from Trips where T.Customer_ID = Customer_ID AND DATEDIFF(d, Trip_EndDate, T.Trip_EndDate) BETWEEN 1 and 7 ) WHEN 0 THEN (Select COUNT(*) from Trips where T.Customer_ID = Customer_ID AND DATEDIFF(d, Trip_EndDate, T.Trip_EndDate) >=7 ) +1 ELSE (Select COUNT(*) from Trips where T.Customer_ID = Customer_ID AND DATEDIFF(d, Trip_EndDate, T.Trip_EndDate) BETWEEN 1 and 7 ) END AS TripID INTO NewTripsfrom Trips T--Update the TripID...Update T SET T.TripID = COALESCE((SELECT TOP 1 TRIPID from NewTrips WHERE T.Customer_ID = Customer_ID AND T.Trip_EndDate> Trip_EndDate order by TRIPID DESC), 1)From NewTrips Twhere TRIPID = 1
HTHEDIT: Poor Format..Edited by - byrmol on 06/02/2003 23:11:26