Ok - I see now. Well this is a little funky but it seems to work:create table #t (thedate datetime primary key clustered, temperature int, grp int)--set up your tabledeclare @MYTABLE table (THEDATE datetime, TEMPERATURE int)insert @mytable (thedate, temperature)select '3/02/2008', 15 union allselect '4/02/2008', 16 union allselect '5/02/2008', 16 union allselect '8/02/2008', 13 union allselect '10/02/2008', 15 union allselect '11/02/2008', 15 union allselect '12/02/2008', 19insert #t (thedate, temperature)select thedate, temperature from @myTabledeclare @temp int ,@grp intselect @temp = -100 ,@grp = 1update #t set @grp = grp = @grp + case when temperature = @temp then 0 else 1 end ,@temp = temperatureselect thedate, temperaturefrom ( select thedate, temperature, dense_rank() over (partition by grp order by thedate) dr from #t ) dwhere dr = 1drop table #toutput:thedate temperature----------------------- -----------2008-03-02 00:00:00.000 152008-04-02 00:00:00.000 162008-08-02 00:00:00.000 132008-10-02 00:00:00.000 152008-12-02 00:00:00.000 19
Be One with the OptimizerTG