9

我尝试了以下方法:

SELECT * FROM generate_series(2,4);
generate_series
-----------------
           2
           3
           4
(3 rows)

SELECT * FROM generate_series(5,1,-2);                                                             
generate_series
-----------------
           5
           3
           1
(3 rows)

但是当我尝试时,

select * from generate_series('2011-12-31'::timestamp, '2012-12-31'::timestamp, '1 day');

它产生了错误。

ERROR:  function generate_series(timestamp without time zone, timestamp without time zone, "unknown") does not exist
HINT:  No function matches the given name and argument types. You may need to add explicit type casts.

我在 Redshift 1.0.757 上使用 PostgreSQL 8.0.2。
知道为什么会这样吗?

更新:

generate_series 现在正在使用 Redshift。

SELECT CURRENT_DATE::TIMESTAMP  - (i * interval '1 day') as date_datetime 
FROM generate_series(1,31) i 
ORDER BY 1

这将生成最近 30 天的日期

4

4 回答 4

20

generate_series()Postgres 8.4 中添加了支持日期和时间戳的版本。

由于 Redshift 基于 Postgres 8.0,因此您需要使用不同的方式:

select timestamp '2011-12-31 00:00:00' + (i * interval '1 day')
from  generate_series(1, (date '2012-12-31' - date '2011-12-31')) i;

如果您“仅”需要日期,则可以缩写为:

select date '2011-12-31' + i
from  generate_series(1, (date '2012-12-31' - date '2011-12-31')) i;
于 2014-03-21T09:17:49.027 回答
6

generate_series 现在正在使用 Redshift。

SELECT CURRENT_DATE::TIMESTAMP  - (i * interval '1 day') as date_datetime 
FROM generate_series(1,31) i 
ORDER BY 1

这将生成最近 30 天的日期

于 2018-06-05T13:29:09.607 回答
2

对于无法使用 generate_series() 在 Redshift 上生成时间维度表的问题,我在这里找到了解决方案。您可以使用以下 SQL 片段生成临时序列。

with digit as (
    select 0 as d union all 
    select 1 union all select 2 union all select 3 union all
    select 4 union all select 5 union all select 6 union all
    select 7 union all select 8 union all select 9        
),
seq as (
    select a.d + (10 * b.d) + (100 * c.d) + (1000 * d.d) as num
    from digit a
        cross join
        digit b
        cross join
        digit c
        cross join
        digit d
    order by 1        
)
select (getdate()::date - seq.num)::date as "Date"
from seq;

似乎 Redshift 还不完全支持 generate_series() 函数。如果我运行 DJo 回答中提到的 SQL,它就可以工作,因为 SQL 只在领导节点上运行。如果我将插入到 dim_time 中添加到相同的 SQL 中,则它不起作用。

于 2019-09-26T10:26:22.547 回答
0

Redshift中没有generate_series()适用于日期范围的功能,但您可以通过以下步骤生成系列...

第 1 步:创建一个表 genid 并插入常量值 1 来表示您需要生成系列的次数。如果您需要生成 12 个月的系列,您可以插入 12 次。更好的是,您可以插入更多次,例如 100 次,这样您就不会遇到任何问题。

create table genid(id int)

------------ 用于插入 genid 值的月数(1)

第 2 步:您需要为其生成系列的表。

create table pat(patid varchar(10),stdt timestamp, enddt timestamp);

insert into pat values('Pat01','2018-03-30 00:00:00.0','2018-04-30 00:00:00.0')

insert into pat values('Pat02','2018-02-28 00:00:00.0','2018-04-30 00:00:00.0')

insert into pat values('Pat03','2017-10-28 00:00:00.0','2018-04-30 00:00:00.0')

第 3 步:此查询将为您生成系列。

with cte as 
(
select max(enddt) as maxdt
from pat
) ,
cte2 as(
select dateadd('month', -1 * row_number() over(order by 1),  maxdt::date ) as gendt  
from  genid , cte
) select * 
from pat, cte2
where gendt between stdt and enddt
于 2018-05-09T07:23:20.567 回答