れ螻 螳 企 .
--drop table #temp
create table #temp(id int, sort_yn int)
insert #temp values(1,0),(2,0),(3,1),(4,1),(5,1),(6,1),(7,0),(8,1),(9,0)
select * from #temp
/*
id sort_yn
----------- -----------
1 0
2 0
3 1
4 1
5 1
6 1
7 0
8 1
9 1
10 0
*/
企 れ螻 螳 trnd = 1 rowる れ朱 螻 . 襯 る れ螻 螳.
id sort_yn
----------- -----------
1 0
2 0
5 1
6 1
4 1
3 1
7 0
9 1
8 1
10 0
れ螻 螳 覃 .
select
id
, sort_yn
from (
select
id
, sort_yn
, seq
, row_number() over(partition by seq order by newid()) sort
from (
select
id
, case when sort_yn = 1 then min(id) over(partition by grp) else id end seq
, sort_yn
, grp
from (
select
id
, sort_yn
, row_number() over(order by id) - row_number() over(order by sort_yn, id) grp
from #temp a
) t
) t
) t
order by seq, sort