»

SQLにおけるリナンバリング(その4)

SQL Server — タグ: , — enjoji.yasujiro @ 11:28 PM

その3の続き

ていうかナンバリング番号を格納したnum列をもう一回Over句でリナンバリングし直すということもSQL Server2005ならば可能だと、さっきふと思った。

with test_Table as(
 select num = 2 ,code = 2121,Period =200503 union all
 select num = 3 ,code = 2121,Period =200603 union all
 select num = 4 ,code = 2121,Period =200703 union all
 select num = 3 ,code = 7203,Period =200303 union all
 select num = 4 ,code = 7203,Period =200403 union all
 select num = 5 ,code = 7203,Period =200503 union all
 select num = 6 ,code = 7203,Period =200603 union all
 select num = 7 ,code = 7203,Period =200703
)
select code
, period
, num
, num1 = rank() over(partition by code order by num)
, num2 = rank() over(order by code,num)
from test_Table
order by code,num


↓実行結果

num:ナンバリングした値(初期値)
num1:ナンバリングした値が格納されているnum列をcodeごとにリナンバリングしたもの
num2:ナンバリングした値が格納されているnum列をテーブル全体でリナンバリングしたもの

1 件のコメント »

  1. [...] さらにその4に続く [...]

このコメント欄の RSS フィード TrackBack URI

コメントをどうぞ

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2009 鎌倉橋日記 | powered by WordPress with Barecity