seq h 1 중분류1 2 소분류1 3 소분류2 4 소분류3 5 중분류2 6 소분류4 7 중분류3 8 소분류5
seq h 2 소분류1 3 소분류2 4 소분류3 1 중분류1 6 소분류4 5 중분류2 8 소분류5 7 중분류3--질문 출처: http://www.sqler.com/bSQLQA/745455
select 1 seq, '중분류1' h into #temp union all select 2, '소분류1' union all select 3, '소분류2' union all select 4, '소분류3' union all select 5, '중분류2' union all select 6, '소분류4' union all select 7, '중분류3' union all select 8, '소분류5'
;with new_idx as ( select h , seq , isnull(lead(seq, 1) over(order by seq), 99999) - 0.5 new_seq from #temp where h like '중%' ) select a.seq , a.h from #temp a left join new_idx b on a.seq = b.seq order by isnull(b.new_seq, a.seq)