Is this what you want ?declare @table table( HistoryTime char(26), TransactionTime char(26), HistoryType char(1), UpdatedHistoryType char(1))insert into @tableselect '2007-03-05 14:46:56.466072', '2006-10-16 15:30:58.853840', 'C', NULL union allselect '2007-03-05 13:48:27.537680', '2006-11-02 12:17:14.673952', 'C', NULL union allselect '2007-03-05 09:00:20.481688', '2006-11-27 12:10:46.379728', 'C', NULL union allselect '2007-03-05 09:04:46.387672', '2006-11-27 12:10:46.379728', 'W', NULL union allselect '2007-03-05 09:05:04.901696', '2006-11-27 12:10:46.379728', 'W', NULL union allselect '2007-03-05 10:00:24.667552', '2006-11-27 12:10:46.379728', 'P', NULL union allselect '2007-03-05 10:05:08.942760', '2006-11-27 12:10:46.379728', 'P', NULL union allselect '2007-03-05 11:00:26.752688', '2006-11-27 12:10:46.379728', 'Q', NULL union allselect '2007-03-05 11:01:59.115160', '2006-11-27 12:10:46.379728', 'Q', NULLupdate tset UpdatedHistoryType = case when t.HistoryType = 'C' then t.HistoryType when t.HistoryType in ('P', 'Q') and n.HistoryTime is not null then t.HistoryType else 'W' endfrom @table tleft join ( select HistoryTime = min(HistoryTime), TransactionTime from @table group by TransactionTime ) m on t.HistoryTime = m.HistoryTime and t.TransactionTime = m.TransactionTimeleft join ( select HistoryTime = min(HistoryTime), TransactionTime, HistoryType from @table where HistoryType in ('P', 'Q') group by TransactionTime, HistoryType ) n on t.HistoryTime = n.HistoryTime and t.TransactionTime = n.TransactionTimeselect *from @table/*HistoryTime TransactionTime HistoryType UpdatedHistoryType -------------------------- -------------------------- ----------- ------------------ 2007-03-05 14:46:56.466072 2006-10-16 15:30:58.853840 C C2007-03-05 13:48:27.537680 2006-11-02 12:17:14.673952 C C2007-03-05 09:00:20.481688 2006-11-27 12:10:46.379728 C C2007-03-05 09:04:46.387672 2006-11-27 12:10:46.379728 W W2007-03-05 09:05:04.901696 2006-11-27 12:10:46.379728 W W2007-03-05 10:00:24.667552 2006-11-27 12:10:46.379728 P P2007-03-05 10:05:08.942760 2006-11-27 12:10:46.379728 P W2007-03-05 11:00:26.752688 2006-11-27 12:10:46.379728 Q Q2007-03-05 11:01:59.115160 2006-11-27 12:10:46.379728 Q W*/
KH