我尝试了以下几点:
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');它产生了错误。
错误:函数generate_series(没有时区的时间戳,没有时区的时间戳,“未知”)不存在提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。
我在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天的日期。
发布于 2018-06-05 13:29:09
generate_series现在正在与Redshift合作。
SELECT CURRENT_DATE::TIMESTAMP - (i * interval '1 day') as date_datetime
FROM generate_series(1,31) i
ORDER BY 1这将产生最后30天的日期。
发布于 2014-03-21 09:17:49
支持日期和时间戳的generate_series()版本是在Postgres8.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;发布于 2019-09-26 10:26:22
我找到了一个解决方案这里,用于解决无法使用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;generate_series()函数在Redshift上似乎还没有完全被支持。如果我运行DJo在答案中提到的SQL,它就能工作,因为SQL只运行在领导节点上。如果我将插入到dim_time的前面添加到相同的SQL中,那么它就不能工作。
https://stackoverflow.com/questions/22554398
复制相似问题