在postgresql函数中动态生成表/数组
我需要创建postgresql函数在postgresql函数中动态生成表/数组,postgresql,Postgresql,我需要创建postgresql函数 CREATE FUNCTION date_ranges (_start date, end date) RETURNING TABLE(day_in_range date) AS... 如果我调用日期范围('2010-06-01',2010-06-05') 我应该收到 2010-06-01 2010-06-02 2010-06-03 2010-06-04 2010-06-05 有什么办法吗?如果您使用的是Postgresql 8.4: SELECT
CREATE FUNCTION date_ranges (_start date, end date)
RETURNING TABLE(day_in_range date) AS...
如果我调用日期范围('2010-06-01',2010-06-05')
我应该收到
2010-06-01
2010-06-02
2010-06-03
2010-06-04
2010-06-05
有什么办法吗?如果您使用的是Postgresql 8.4:
SELECT generate_series(_start ::timestamp,_end ::timestamp,'1 day');
例如:
postgres=# SELECT generate_series('2010-06-01'::timestamp,
postgres-# '2010-06-05'::timestamp,'1 day')::date;
generate_series
-----------------
2010-06-01
2010-06-02
2010-06-03
2010-06-04
2010-06-05
在旧版本上:
SELECT '2010-06-01'::date + step FROM
generate_series(0,'2010-06-05'::date - '2010-06-01'::date,1) AS t(step);
看看最后一个例子:WorksPerfect-谢谢(特别是我使用的旧版本8.1)-bensiu8.1将在今年下线,开始升级到新版本。