簡述:我需要在Redshift中創建一個表,該表是一個從今天起一年內所有秒數的時間戳的串列,雙向的。
條件:
- 我不能使用
generate_series,因為我需要將結果寫到一個表中。generate_series不允許我寫到表中,因為它只是一個領導節點,在Redshift中不支持。 - 我沒有另一個表中的時間戳串列可以參考。
- 由于檔案大小的原因,匯出結果并匯入到一個新的表中是不可行的。我特別需要一個獨立的解決方案。
我希望,但不需要:
- 一個允許我做
generate_series所做的事情而不受當前限制的函式。我不確定這是否可行,也不確定如何撰寫,因為我認為Redshift真的不允許以我想要的方式進行回圈,但希望我是錯的。
期望的輸出示例:
20210913 00: 01: 04. 000000:01:04.
2021-09-13 00:01:03.0000002021-09-13 00:01:02.0000002021-09-13 00:01:01.0000002021-09-13 00:01:00.000000
是否有辦法用某種for回圈或n 1型別的解決方案來做這個?我非常愿意在Epoch time中做這件事,然后像上面那樣轉換為timestamp型別,我只是需要一個可行的解決方案,讓我能夠寫到一個表中。生成該示例輸出的代碼如下:
with interval_1_second_cte as (
SELECT CURRENT_DATE:: TIMESTAMP - (i * interval '1 seconds'/span>) as interval_1_second
FROM generate_series(1, (365 * 24 * 60 * 60) i
UNION ALL
SELECT CURRENT_DATE:: TIMESTAMP (i * interval '1 seconds'/span>) as interval_1_second
FROM generate_series(1, (365 * 24 * 60 * 60) i
)
select top 5 i1sc.interval_1_second
from interval_1_second_cte i1sc
where interval_1_secondlike '2021-09-13 00:01:0'
order by 1;
uj5u.com熱心網友回復:
我不明白 "在兩個方向 "是什么意思,除了一個當前年份的所有秒數的串列。
這可以通過一個遞回的普通表運算式來實作:
with recursive This_year as (
SELECT date_trunc('year' , current_timestamp) as ts
UNION ALL
SELECT p.ts interval '1 seconds'
from this_year p
where p.ts < date_trunc('year'/span>, current_timestamp) interval '1 year')
)
select*
from this_year
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/309263.html
標籤:
下一篇:PostgreSQL。函式不存在
