簡體   English   中英

在postgresql的一欄中生成系列月份

[英]Generate series of months in a column at postgresql

在postgresql中,如何生成格式為“ YYYY-MM”的一系列每月日期,最舊的是用戶的創建月份,直到當前月份?

就像是 :

select to_char(dt, 'YYYY-MM') 
from generate_series(
     date_trunc('month', (select created_at::date from users where id=1234)),
     now(), 
     '1 month'::interval) dt;

您甚至可以在單個查詢級別中執行此操作:

SELECT to_char(generate_series(created_at::date
                              ,now(), interval '1 mon'), 'YYYY-MM') AS month
FROM   users
WHERE  users_id = 123   -- users_id is unique

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM