假设我想在 24 小时内每 5 分钟生成一个系列。我如何在 PostgreSQL 中做到这一点?
PostgreSQL 可以generate_series()来自 a timestamp,但不能来自time.
选择任意时间戳更好,还是有另一种生成系列的方法?
Erw*_*ter 19
优化:
SELECT x::time
FROM generate_series(timestamp '2000-01-01 00:00'
, timestamp '2000-01-02 00:00'
, interval '5 min') t(x);
Run Code Online (Sandbox Code Playgroud)
日期无关紧要,因此请使用任意时间戳常量。演员阵容time非常便宜。
这包括下限和上限,所以我们得到了'00:00' 两次。使用'2000-01-01 23:59'作为上限仅一次得到它。
有关的:
不确定这是否是最好的方法,但我们可以使用它generate_series来生成最小偏移量00:00:00,然后简单地调用make_interval(mins=>)以从中获取间隔。
SELECT make_interval(mins=>x)::time
FROM generate_series(0, 60*24-5, 5) AS t(x);
Run Code Online (Sandbox Code Playgroud)
我喜欢 @EvanCarroll 方式,但还有另一种选择 -
select x::time
from generate_series
(current_date,current_date + '1 day - 1 second'::interval,'5 minute') as t(x);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11923 次 |
| 最近记录: |