到着セリ
実家から食べ物とかが送られてきた。
何かコンサル様みたい(笑)
ここ数年資料やマニュアルを全く作らない&会議もなるべく出ないみたいなことを仕事でやらかしてたので、久々にエクセルでせっせと図表を作る作業に若干手間取っております。ていうかエクセルってここ数年はVBAマクロしか使ってないから全然勝手が違うがな。
その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
その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 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
クエリー結果↓
TUTAYAの会員関係とかBフレッツの切替関係とか源泉徴収関係の税金の書類とか丸三証券の口座用とか、いろいろ書類を作成しなくてはならないのですが、なのに印鑑の朱肉が持ち合せておらず今日の今日までずーと書類作成は放置プレイにしてたのですが、つい今しがたようやく朱肉をゲットしました。しかも高島屋で。
あけおめのことよろな感じの二千八年正月、みなさんいかがお過ごしでしょうか?
僕は実家の岐阜に帰省してました。超寒かったとです。でも去年帰省した時は風邪引いてしまい、無理して4日に東京に戻ってきて体調絶不調のままmixiのオフ会に参加してテンション超低いまま盛り上がれないまま退散(←これが2007年のすべてを物語ってたかも?と今思う)したのですが、今年は4日に有休取って風邪もひかずに万全の体調で戻ってまいりましたです。はい。
ということで明日から仕事始め。とにもかくにもそろそろ再始動&復活させねばなるまいて。