简体   繁体   English

如何将列添加到generate_series查询

[英]How to add column to a generate_series query

When looking a date ranges, is there anyway to have generate_series return the starting and ending dates as well? 当查找日期范围时,是否仍然要让generate_series返回起始日期和结束日期?

select 
    '2014-06-05 00:00:00'::timestamp + ('1 month'::INTERVAL * s.a) AS date 
from 
    generate_series(1, cast(extract(month from age('2014-09-05'::date, '2014-06-05'::date)) AS integer), 1) as s(a);

Gives this output 提供此输出

date
2014-07-05 00:00:00
2014-08-05 00:00:00
2014-09-05 00:00:00

This is fine, however I would like to have 很好,但是我想要

start_date     end_date       date
2014-06-05    2014-09-05    2014-07-05 00:00:00
2014-06-05    2014-09-05    2014-08-05 00:00:00
2014-06-05    2014-09-05    2014-09-05 00:00:00

The reason being is that I am extracting multiple start/end pairs from another table but cannot figure out a way of joining them together. 原因是我正在从另一个表中提取多个开始/结束对,但无法找出将它们连接在一起的方法。 I am also using PostgeSQL version 8.2.15 (hence the more complicated generate_series function). 我还使用了PostgeSQL版本8.2.15(因此使用了更复杂的generate_series函数)。

To expand this to my primary problem, I have a table that contains these start and end time pairs. 要将其扩展到我的主要问题,我有一个包含这些开始时间和结束时间对的表。

    start_date         end_date
2014-08-25 00:00:00 2014-09-25 00:00:00
2014-05-16 00:00:00 2014-08-16 00:00:00
2014-09-09 00:00:00 2014-12-09 00:00:00
2014-06-05 00:00:00 2014-07-05 00:00:00
2014-05-19 00:00:00 2014-08-19 00:00:00
2014-05-15 00:00:00 2014-07-15 00:00:00
2014-09-04 00:00:00 2014-11-04 00:00:00

How can I iterate through this table and join it with the expanded date ranges? 如何遍历此表并将其与扩展的日期范围结合在一起?

Consider upgrading to a current version . 考虑升级到当前版本 Postgres 8.2 is long dead and forgotten. Postgres 8.2早已死了,被人们遗忘了。

For Postgres 8.2 (or later, but there are more elegant solutions in modern Postgres). 对于Postgres 8.2(或更高版本,但现代Postgres中有更多优雅的解决方案)。

Assuming it's all about dates , not timestamps. 假设所有有关日期 ,而不是时间戳。

Providing start_date and end_date once : 提供一次 start_dateend_date

SELECT start_date, end_date
     , (start_date + interval '1 month'
                   * generate_series(1, months))::date AS the_date
FROM  (  
   SELECT extract(month from age(end_date, start_date))::int AS months
        , start_date, end_date
   FROM (SELECT '2014-06-05'::date AS start_date
              , '2014-09-05'::date AS end_date
        ) data
   ) sub;

Using column name the_date instead of date which shouldn't be used as identifier. 使用列名the_date代替不应用作标识符的date

Drawing values from table t instead: 而是从表t绘制值:

SELECT start_date, end_date
     ,(start_date + interval '1 month'
                  * generate_series(1, months))::date AS the_date
FROM  (SELECT *, extract(month FROM age(end_date, start_date))::int AS months
       FROM   t) sub;

Without subquery 没有子查询

SELECT t_id, start_date, end_date
     ,(start_date + interval '1 month'
                  * generate_series(1, extract(month from age(end_date
                                                            , start_date))::int)
                  )::date AS the_date
FROM   t;

SQL Fiddle for Postgres 8.3. 适用于Postgres 8.3的SQL Fiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM