在 redshift 中使用 sql 函数 generate_series()

新手上路,请多包涵

我想在redshift中使用生成系列功能,但没有成功。

红移文档说它不受支持。以下代码确实有效:

 select *
from generate_series(1,10,1)

输出:

 1
2
3
...
10

我想对日期做同样的事情。我尝试了许多变体,包括:

 select *
from generate_series(date('2008-10-01'),date('2008-10-10 00:00:00'),1)

踢出:

  ERROR: function generate_series(date, date, integer) does not exist
 Hint: No function matches the given name and argument types.
 You may need to add explicit type casts. [SQL State=42883]

也试过:

 select *
from generate_series('2008-10-01 00:00:00'::timestamp,
'2008-10-10 00:00:00'::timestamp,'1 day')

并尝试:

 select *
from generate_series(cast('2008-10-01 00:00:00' as datetime),
cast('2008-10-10 00:00:00' as datetime),'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. [SQL State=42883]

如果不是,我将使用另一篇文章中的此代码:

 SELECT to_char(DATE '2008-01-01'
+ (interval '1 month' * generate_series(0,57)), 'YYYY-MM-DD') AS ym

PostgreSQL generate_series() 以 SQL 函数作为参数

原文由 Elm 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 602
1 个回答

对于五分钟的存储桶,我将执行以下操作:

 select date_trunc('minute', getdate()) - (i || ' minutes')::interval
from generate_series(0, 60*5-1, 5) as i

您可以将 5 替换为任何给定的间隔,将 60 替换为所需的行数。

原文由 Guilherme Santos 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
Stack Overflow 翻译
子站问答
访问
宣传栏