»

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

SQL Server — タグ: , — enjoji.yasujiro @ 8:59 PM

その2からの続き

SQL Server2005から、順位付け関数(Over句)が用意されているので、これを利用すれば何も自己結合でゴキンゴキンにややこしいjoinなんかしなくても超簡単にリナンバリングは可能。

with test_Table as(
select code = 1301,Period = 200303 union all
select code = 1301,Period =200403 union all
select code = 1301,Period =200503 union all
select code = 1301,Period =200603 union all
select code = 1301,Period =200703 union all
select code = 1301,Period =200803 union all
select code = 2121,Period =200609 union all
select code = 2121,Period =200709 union all
select code = 2121,Period =200809 union all
select code = 7203,Period =200303 union all
select code = 7203,Period =200403 union all
select code = 7203,Period =200503 union all
select code = 7203,Period =200603 union all
select code = 7203,Period =200703 union all
select code = 7203,Period =200803
)select *
, num1 = rank() over(partition by code order by period)
, num2 = rank() over(order by Code ,period)
from test_Table

(続きを読む…)

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

SQL Server — タグ: , — enjoji.yasujiro @ 8:55 PM

その1からの続き

あるいはデカルト積チックに自己結合して、同じcode内にてPeriodが同じまたはPeriodが小さいレコードをjoinして、それをGroup byでレコード数をカウントするという方法もあるかも。

with test_Table as(
    select code = 1301,Period = 200303 union all
    select code = 1301,Period =200403 union all
    select code = 1301,Period =200503 union all
    select code = 1301,Period =200603 union all
    select code = 1301,Period =200703 union all
    select code = 1301,Period =200803 union all
    select code = 2121,Period =200609 union all
    select code = 2121,Period =200709 union all
    select code = 2121,Period =200809 union all
    select code = 7203,Period =200303 union all
    select code = 7203,Period =200403 union all
    select code = 7203,Period =200503 union all
    select code = 7203,Period =200603 union all
    select code = 7203,Period =200703 union all
    select code = 7203,Period =200803
)select num=count(b.period)
, a.code
, a.period
from test_Table a
inner join test_Table b
on a.code = b.code
and a.period >= b.period
group by a.code,a.period
order by a.code,a.period

(続きを読む…)

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

SQL Server — タグ: , — enjoji.yasujiro @ 8:53 PM

例えばSQL Server2000までなら、以下のようにAutoNum付き#tempテーブルを作ってデータを任意のソート順で格納してSEELCT文を発行(最後に#tempテーブルをdrop tableで破棄)してあげる方法でナンバーリングが可能だったと思います。

サンプル↓

–仮テーブル生成 ・・・①
create table #test(
RecordID int identity(1,1)
, code int
, period int
)

–データ格納 ・・・②
insert into #test
select code = 1301,Period = 200303 union all
select code = 1301,Period =200403 union all
select code = 1301,Period =200503 union all
select code = 1301,Period =200603 union all
select code = 1301,Period =200703 union all
select code = 1301,Period =200803 union all
select code = 2121,Period =200609 union all
select code = 2121,Period =200709 union all
select code = 2121,Period =200809 union all
select code = 7203,Period =200303 union all
select code = 7203,Period =200403 union all
select code = 7203,Period =200503 union all
select code = 7203,Period =200603 union all
select code = 7203,Period =200703 union all
select code = 7203,Period =200803

–検索 ・・・③
select * from #test

–破棄 ・・・④
drop table #test

クエリー結果↓

(続きを読む…)

朱肉@日本橋髙島屋

Diary — enjoji.yasujiro @ 3:00 PM

TUTAYAの会員関係とかBフレッツの切替関係とか源泉徴収関係の税金の書類とか丸三証券の口座用とか、いろいろ書類を作成しなくてはならないのですが、なのに印鑑の朱肉が持ち合せておらず今日の今日までずーと書類作成は放置プレイにしてたのですが、つい今しがたようやく朱肉をゲットしました。しかも高島屋で。

(続きを読む…)

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