This is my sql:declare @Realtime table( Symbol varchar(3), [Date] datetime, Value int)insert into @Realtimeselect 'ABC', '1/3/2009 03:05:01', 327 union all -- this day is not existed in EOD -> insertingselect 'BBC', '1/3/2009 03:05:01', 458 union all -- this day is not existed in EOD -> insertingselect 'ABC', '1/2/2009 03:05:01', 326 union all -- this day is new -> updatingselect 'BBC', '1/2/2009 03:05:01', 454 union all -- this day is new -> updatingselect 'ABC', '1/2/2009 02:05:01', 323 union allselect 'BBC', '1/2/2009 02:05:01', 453 union allselect 'ABC', '1/2/2009 01:05:01', 313 union allselect 'BBC', '1/2/2009 01:05:01', 423SELECT Symbol, [Date], ValueFrom( Select Symbol, [Date], Value, row_no = ROW_NUMBER() OVER (PARTITION BY Symbol ORDER BY Date DESC) FROM @Realtime)rWhere r.row_no = 1
Thanks.