LoginSignup
8
9

More than 5 years have passed since last update.

#PostgreSQL Window関数で投稿数の累計を抽出

Posted at

背景

  • 日付 ごとの その日の投稿数その日までの投稿数 を抽出したいことから。

Window関数を用いたSQL

  • 投稿された日: created TIMESTAMP WITHOUT TIME ZONE
SELECT
    DATE(table.created) AS "日付",
    COUNT(*) AS "その日の投稿数",
    SUM(COUNT(*)) OVER(ORDER BY DATE(table.created) ASC) AS "その日までの投稿数"
FROM table
GROUP BY DATE(table.created)

結果

日付 その日の投稿数 その日までの投稿数
2016-09-01 1 1
2016-09-02 3 4
2016-09-03 2 6
2016-09-04 3 9
2016-09-05 3 12

その他 Window関数を用いたSQL

8
9
0

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
8
9