【问题标题】:Amazon Redshift: How to create a table containing time seriesAmazon Redshift:如何创建包含时间序列的表
【发布时间】:2017-10-01 20:11:04
【问题描述】:

我想在 Redshift 中创建一个表,其中有一个列 date,其值将从今天到未来 3 年。

        date         
---------------------
 2017-05-03 00:00:00
 2017-05-04 00:00:00
 2017-05-05 00:00:00
 2017-05-06 00:00:00

我正在尝试使用create table as select_query 语句来执行此操作。

 create table tmp_date_series as select now()::date::timestamp + generate_series(0, 1000);

但是,上面的查询失败并出现错误 -

INFO:  Function "now()" not supported.
INFO:  Function "generate_series(integer,integer)" not supported.
ERROR:  Specified types or functions (one per INFO message) not supported on Redshift tables.

但如果我单独运行选择查询 - select now()::date::timestamp + generate_series(0, 150) as date;,它运行时不会出现任何错误 -

        date         
---------------------
 2017-05-03 00:00:00
 2017-05-04 00:00:00
 2017-05-05 00:00:00

知道如何创建这样的表吗?

【问题讨论】:

  • 投票重新开放,generate_series 在 AWS Redshift 上不可用。
  • 不应该generate_series() 进入from 子句吗? create table x as select (current_date + i)::timestamp from generate_series(0,1000) as x(i)
  • 顺便说一句,您问题中的查询实际上有效,因为它仅在领导节点上运行,该节点使用 PostgreSQL 8.4 的分支。一旦您点击工作节点(例如,如果您使用任何“真实”表),查询就会在那些没有 generate_series 的节点上运行。
  • 您必须使用带有数字或 cte 的表格,而不是 generate_seriesstackoverflow.com/questions/38667215/…

标签: sql amazon-redshift


【解决方案1】:

很遗憾,Redshift 不支持generate_series

我的团队正在使用这样的 CTE 来获取从特定日期开始的一系列连续日期:

with dates as (
    select
        (trunc(getdate()) + row_number() over (order by 1))::date as date
    from
        large_enough_table
    limit
        150
)

那么你可以把它当作:

select date
from dates
order by date
limit 5

得到:

2018-12-13
2018-12-14
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 2019-08-01
    相关资源
    最近更新 更多