Man, this is an odd requirement ...How bout this ...drop table alexcgocreate table alexc ( row_id int not null primary key, row_value int )goinsert into alexc (row_id,row_value)select 1,6union select 2,6union select 3,2union select 4,2union select 5,7union select 6,4union select 7,4union select 8,6union select 9,10union select 10,10goselect start_id, (select min(row_id) from alexc a where row_id > s.start_id and row_value between 5 and 10 and exists ( select 1 from alexc where a.row_id - 1 = row_id and row_value not between 5 and 10)) as end_idfrom ( select row_id as start_id from alexc a where row_value between 5 and 10 and exists ( select 1 from alexc where a.row_id + 1 = row_id and row_value not between 5 and 10) ) sgo
EDIT: or a bit more clearly ...select row_id as start_id, (select min(row_id) from alexc b where row_id > a.row_id and row_value between 5 and 10 and exists ( select 1 from alexc where b.row_id - 1 = row_id and row_value not between 5 and 10)) as end_idfrom alexc awhere row_value between 5 and 10 and exists ( select 1 from alexc where a.row_id + 1 = row_id and row_value not between 5 and 10)go
Jay White{0}Edited by - Page47 on 10/16/2002 13:32:42