LoginSignup
0
0

Postgresql で連続した日付を作成する方法

Last updated at Posted at 2023-08-23

普段 orm で済ましているから機会がなかったが、SQLだけでクエリ〜集計まで全てやることがあったので備忘録として

generate_series を使用して作成。当然 PostgreSQL 専用。
now()interval の範囲を変えればいろんなケースに対応可能。

-- 1年以内の yyyy-MMのリスト
with year_month_list as (
  select 
    to_char(generate_series, 'yyyy-MM') as date
  from 
    generate_series(
      (now() - interval '1 year') at time zone 'jst', 
      now() at time zone 'jst',
      '1 month')
)
0
0
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
0
0