SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

DeveloperZine(デベロッパージン)- エンジニアの意思決定を支える技術情報メディア ProductZine

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

分析関数の衝撃

PostgreSQLの分析関数の衝撃(8)
――RowsとRangeの代用

OracleのRowsやRangeの代用

ダウンロード SourceCode (1.6 KB)

Rows between 2 following and unbounded following

 応用して今度は、Rows between 2 following and unbounded followingcount(*)minmaxsumを代用してみます。サンプルを見てみましょう。

模倣対象のOracleのSQL
select sortKey,Val,
count(*) over(order by sortKey
              Rows between 2 following
                       and unbounded following) as cnt,
min(Val) over(order by sortKey
              Rows between 2 following
                       and unbounded following) as minVal,
max(Val) over(order by sortKey
              Rows between 2 following
                       and unbounded following) as maxVal,
sum(Val) over(order by sortKey
              Rows between 2 following
                       and unbounded following) as sumVal
  from OracleCompOlap
order by sortKey;
出力結果
sortKey Val cnt minVal maxVal sumVal
1 1 10 0 9 50
3 4 9 0 9 45
5 5 8 0 8 36
7 9 7 0 7 28
8 8 6 0 7 26
9 2 5 3 7 26
10 0 4 3 7 21
12 5 3 3 6 14
13 7 2 5 6 11
14 3 1 6 6 6
15 5 0 null null null
18 6 0 null null null

 本稿の「1. Rows 2 Preceding」と「2. Rows between current row and unbounded following」を踏まえて、答えは下記となります。なお、array_agg関数は、集約の内訳を表示するのに便利なので使用してます。

PostgreSQL8.4での代用案
select sortKey,Val,
Lead(WKcnt,2,0::bigint) over(order by sortKey) as cnt,
Lead(WKminVal,2) over(order by sortKey) as minVal,
Lead(WKmaxVal,2) over(order by sortKey) as maxVal,
Lead(WKsumVal,2) over(order by sortKey) as sumVal,
Lead(WKVals,2)   over(order by sortKey) as Vals
from (select sortKey,Val,
      count(*) over(order by sortKey desc) as WKcnt,
      min(Val) over(order by sortKey desc) as WKminVal,
      max(Val) over(order by sortKey desc) as WKmaxVal,
      sum(Val) over(order by sortKey desc) as WKsumVal,
      array_agg(Val) over(order by sortKey desc) as WKVals
        from OracleCompOlap) a
order by sortKey;
出力結果
sortKey Val cnt minVal maxVal sumVal Vals
1 1 10 0 9 50 {6,5,3,7,5,0,2,8,9,5}
3 4 9 0 9 45 {6,5,3,7,5,0,2,8,9}
5 5 8 0 8 36 {6,5,3,7,5,0,2,8}
7 9 7 0 7 28 {6,5,3,7,5,0,2}
8 8 6 0 7 26 {6,5,3,7,5,0}
9 2 5 3 7 26 {6,5,3,7,5}
10 0 4 3 7 21 {6,5,3,7}
12 5 3 3 6 14 {6,5,3}
13 7 2 5 6 11 {6,5}
14 3 1 6 6 6 {6}
15 5 0 null null null null
18 6 0 null null null null

 SQLのイメージは下記となります。order by sortKey Rows between 2 following and unbounded followingに対応する紫線と黄緑線と青線を引いてます。

SQLのイメージ
SQLのイメージ

次のページ
4. Range 3 Preceding

この記事は参考になりましたか?

分析関数の衝撃連載記事一覧

もっと読む

この記事の著者

山岸 賢治(ヤマギシ ケンジ)

趣味が競技プログラミングなWebエンジニアで、OracleSQLパズルの運営者。AtCoderの最高レーティングは1204(水色)。

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

この記事は参考になりましたか?

この記事をシェア

CodeZine(コードジン)
https://codezine.jp/article/detail/4848 2010/04/13 14:00

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー