>> I don't think it can be done in a single statement.This is sqlteam.create table #quote( tradeDay datetime ,[min] smallint ,symbol varchar(8) ,openPx numeric(11,4) ,highPx numeric(11,4) ,lowPx numeric(11,4) ,closePx numeric(11,4) ,volume int ,ticks int)insert #quoteselect '3/4/2005','0','MSFT','25.2','25.2','25.19','25.2','3990','9' union allselect '3/4/2005','1','MSFT','25.19','25.25','25.19','25.2','2300','6' union allselect '3/4/2005','2','MSFT','25.19','25.2','25.18','25.2','1620','5' union allselect '3/4/2005','3','MSFT','25.19','25.2','25.19','25.2','8315','21' union allselect '3/4/2005','4','MSFT','25.19','25.2','25.19','25.2','790','5' union allselect '3/4/2005','5','MSFT','25.2','25.2','25.17','25.2','4485','10' union allselect '3/4/2005','6','MSFT','25.2','25.26','25.19','25.19','6923','12' union allselect '3/4/2005','7','MSFT','25.19','25.2','25.19','25.1901','6588','12'declare @N smallintset @N = 4select tradeDay ,symbol ,[min] as [min] ,max(case when rem = 0 then openPx else -999 end) as openPx ,max(highPx) as highPx ,min(lowPx) as lowPx ,max(case when rem = @N-1 then closePx else -999 end) as closePx ,sum(volume) as volume ,sum(ticks) as ticksfrom ( select tradeDay,@N*([min]/@N) as [min],[min]%@N as rem, symbol,openPx,highPx,lowPx,closePx,volume,ticks from #quote ) as derivedquotegroup by tradeDay ,[min] ,symbolhaving count(*) = @Norder by tradeDay ,[min] ,symboldrop table #quote
rockmoose