LoginSignup
9
9

More than 3 years have passed since last update.

QUERY関数で「月ごと」に集計したい

Last updated at Posted at 2019-06-04

(追記 2020/10/24)
QUERY関数で「年月ごと」に集計したい の記事のほうがニーズに合うかもしれません。


GoogleスプレッドシートのQUERY関数をつかって、「月ごと」に集計したいケースに出会ったのですが、ちと戸惑ったので自分メモ。わかってれば何でもないのだろうけれども。

やりたいこと

こんな表があったとして
01.png

↓ これを出したい
02.png

やったこと

=QUERY($A$1:$B,"
  select month(A)+1, sum(B) 
  where A is not null 
  group by month(A)+1
", true)

ってやると↓こうなる
03.png

※コメントいただいたように、month(A)+1する理由は、Query関数のmonth関数はゼロベースでのカウント(0が1月。1が2月)なので、month(5) は 4月を表します。

ラベルを付けたいのでこうする↓

=QUERY($A$1:$B,"
  select month(A)+1, sum(B) 
  where A is not null 
  group by month(A)+1 
  label month(A)+1 '月', sum(B) '合計'
", true)

04.png

うん。QUERY関数楽しい。(もっとスマートな書き方があったら教えて!)

9
9
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9