Postgresql 生成for循环中使用的日期序列

Postgresql 生成for循环中使用的日期序列,postgresql,time-series,Postgresql,Time Series,我有一系列按日期划分的表,并按以下格式命名: public.schedule_20121018 是否有方法在上述20121018模式中生成一系列日期,以便我可以通过信息\u模式表执行for循环SELECT 现在我有 SELECT * FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = 'public' AND table_name like 'schedule_%' ORDER BY

我有一系列按日期划分的表,并按以下格式命名:

public.schedule_20121018
是否有方法在上述
20121018
模式中生成一系列日期,以便我可以通过
信息\u模式表执行for循环
SELECT

现在我有

SELECT * FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
AND table_schema = 'public'
AND table_name like 'schedule_%'
ORDER BY table_name;
但例如,我需要最后7天的记录,以便日期顺序从
20121012
开始到
20121018
。谢谢

SELECT *
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
    AND table_schema = 'public'
    AND table_name in (
        select 'schedule_' || to_char(d, 'YYYYMMDD')
        from 
        generate_series(current_date - 7, current_date - 1, '1 day') s(d)
        )
ORDER BY table_name;
较旧的Postgresql版本:

SELECT *
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
    AND table_schema = 'public'
    AND table_name in (
        select 'schedule_' || to_char(current_date - d, 'YYYYMMDD')
        from 
        generate_series(7, 1, -1) s(d)
        )
ORDER BY table_name;
较旧的Postgresql版本:

SELECT *
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
    AND table_schema = 'public'
    AND table_name in (
        select 'schedule_' || to_char(current_date - d, 'YYYYMMDD')
        from 
        generate_series(7, 1, -1) s(d)
        )
ORDER BY table_name;

使用
generate_series
,可能使用
to_char
进行格式化

regress=# select generate_series(DATE '20121012', DATE '20121018', interval '1' day);
    generate_series     
------------------------
 2012-10-12 00:00:00+08
 2012-10-13 00:00:00+08
 2012-10-14 00:00:00+08
 2012-10-15 00:00:00+08
 2012-10-16 00:00:00+08
 2012-10-17 00:00:00+08
 2012-10-18 00:00:00+08
(7 rows)

使用
generate_series
,可能使用
to_char
进行格式化

regress=# select generate_series(DATE '20121012', DATE '20121018', interval '1' day);
    generate_series     
------------------------
 2012-10-12 00:00:00+08
 2012-10-13 00:00:00+08
 2012-10-14 00:00:00+08
 2012-10-15 00:00:00+08
 2012-10-16 00:00:00+08
 2012-10-17 00:00:00+08
 2012-10-18 00:00:00+08
(7 rows)

谢谢你,克洛多尔多。我尝试了
to_char
,但它给出了
错误:函数“to_char”
有多个小数点。你在你的机器上试过了吗?或者我应该使用其他功能吗?我怀疑是因为我使用的是较低版本的Postgresql 8.2?谢谢@Rock为较旧的postgresql版本添加了一个版本。非常感谢Clodoaldo。我用另一种方法
从generate_series(-7,-1,1)中选择regexp_replace((current_date+s.a),“-”,“g”)作为s(a)的日期to_char
,但它给出了
错误:函数“to_char”
有多个小数点。你在你的机器上试过了吗?或者我应该使用其他功能吗?我怀疑是因为我使用的是较低版本的Postgresql 8.2?谢谢@Rock为较旧的postgresql版本添加了一个版本。非常感谢Clodoaldo。我用另一种方法
从generate_series(-7,-1,1)中选择regexp_replace((current_date+s.a),“-”,“g”)作为s(a)的日期