1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[MySQL] 月ごとのカウント抽出で歯抜けになっている月を埋める

Last updated at Posted at 2020-09-09

やりたいこと

月ごとのレコード数のカウントを取りたいが、レコードのない月でもカウント0で出力したい
日付マスタみたいなテーブルは作りたくない。SQLだけで完結させたい

対象のテーブル

データ

usersテーブル

created_at
2020-09-01
2020-09-01
2020-07-01
2020-07-01
2020-05-01

普通にSELECTした場合の出力

カウント
202009 2
202007 2
202005 1

以下のような出力が欲しい

カウント
202009 2
202008 0
202007 2
202006 0
202005 1

歯抜け対応のSQL

月一覧を出力する文を作成して、UNION ALLすることで対応する

SELECT
    EXTRACT(YEAR_MONTH FROM DATE_ADD(NOW(), INTERVAL(months.series) MONTH )) AS yearMonth
  FROM (
    SELECT 0 AS series
    FROM DUAL
    WHERE (@num := 1) * 0
    UNION ALL
    SELECT @num := @num - 1
    FROM information_schema.COLUMNS
    LIMIT 12
  ) AS months

最終的なSQLはこちら

  • 月一覧取得の方に0 AS count を追加
SELECT
  s.yearMonth AS yearMonth,
  SUM(s.count) AS count
FROM (
  SELECT
    EXTRACT(YEAR_MONTH FROM DATE_ADD(NOW(), INTERVAL(months.series) MONTH )) AS yearMonth,
    0 AS count
  FROM (
    SELECT 0 AS series
    FROM DUAL
    WHERE (@num := 1) * 0
    UNION ALL
    SELECT @num := @num - 1
    FROM information_schema.COLUMNS
    LIMIT 12
  ) AS months
  UNION ALL
  SELECT
    EXTRACT(YEAR_MONTH FROM users.created_at) AS yearMonth,
    COUNT(*) AS count
  FROM
    users
  GROUP BY yearMonth
) AS s
GROUP BY s.yearMonth
ORDER BY s.yearMonth DESC
LIMIT 12

追記

MySQL8の場合は、以下のコメントにあるWITH句を使った方法がわかりやすいです!

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?