_ | 覦覈襦 | 豕蠏手 | 殊螳 | 譯殊碁
FrontPage › 覿覿
sqler.com [http]覩語 讌覓 殊 企瓦.

れ螻 螳 企 .
--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
蠍 蠍郁鍵..
企: : るジ讓曙 襦螻豺 企Ν 譯殊語. 襦螻豺
EditText : Print : Mobile : FindPage : DeletePage : LikePages : Powered by MoniWiki : Last modified 2018-04-13 23:12:53

讌れ 觜 譯 螳れ蠍磯 讌襷 蟆一 蟶殊讌 .