sql - 使用 generate_series 填充 PostgreSQL 中给定年份的月份列表

标签 sql database postgresql generate-series

当输入为日期 (2014-01-01) 时,我有以下查询生成给定年份的月份列表:

SELECT to_char(dd, 'Month') 
FROM generate_series(CAST('2014-01-01' AS DATE), date_trunc('month', now()), '1 month') as dd;

  to_char  
-----------
 January  
 February 
(2 rows)

现在,我只需要传递 2014 而不是 2014-01-01 并获得相同的结果。

最佳答案

SELECT distinct to_char(dd, 'Month') FROM generate_series(to_date('2013', 'YYYY') , date_trunc('month', now()), '1 month') as dd;

关于sql - 使用 generate_series 填充 PostgreSQL 中给定年份的月份列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21933364/

相关文章:

SQL Server : concatenate data result in to a table format

MySQL:克隆表 A 中的多行和表 B 中的相关行

MYSQL返回其他数据库表中不存在的行ID列表

java - 在java中获取无效游标状态异常

mysql - 使用 Wampserver 安装 WordPress 时出现““Can’ t 选择数据库”错误

mysql - 一对一关系还是使用同一张表?

postgresql - 这个语句会命中数据库吗?

mysql - phpmyadmin 下划线赋予错误数据库权限

postgresql - 我应该如何打开 PostgreSQL 转储文件并向其中添加实际数据?

sql - 使用 pg_dump 从一个模式导出并导入到另一个模式