在 Snowflake 中以 15 分钟的日期时间间隔创建 table
Create table with 15 minutes interval on date time in Snowflake
我正在尝试以 15 分钟的间隔在 Snowflake 中创建一个 table。我试过使用发电机,但在 15 分钟的间隔内没有给出。有没有什么函数可以用来生成和构建这个 table 几年的有价值的数据。
比如
Date
Hour
202-03-29
02:00 AM
202-03-29
02:15 AM
202-03-29
02:30 AM
202-03-29
02:45 AM
202-03-29
03:00 AM
202-03-29
03:15 AM
.........
........
.........
........
谢谢
使用以下作为间隔 15 分钟的时间生成器,然后根据需要使用其他日期时间函数在单独的列中提取日期部分或时间部分。
with CTE as
(select timestampadd(min,seq4()*15 ,date_trunc(hour, current_timestamp())) as time_count
from table(generator(rowcount=>4*24)))
select time_count from cte;
+-------------------------------+
| TIME_COUNT |
|-------------------------------|
| 2022-03-29 14:00:00.000 -0700 |
| 2022-03-29 14:15:00.000 -0700 |
| 2022-03-29 14:30:00.000 -0700 |
| 2022-03-29 14:45:00.000 -0700 |
| 2022-03-29 15:00:00.000 -0700 |
| 2022-03-29 15:15:00.000 -0700 |
.
.
.
....truncated output
| 2022-03-30 13:15:00.000 -0700 |
| 2022-03-30 13:30:00.000 -0700 |
| 2022-03-30 13:45:00.000 -0700 |
+-------------------------------+
这个问题已经有很多答案 (这 4 个都是这个月的)。
但要注意的主要一点是您不得使用 SEQx()
作为数字生成器(您可以在 ORDER BY 中使用它,但这不是必需的)。如 doc's
中所述
Important
This function uses sequences to produce a unique set of increasing integers, but does not necessarily produce a gap-free sequence. When operating on a large quantity of data, gaps can appear in a sequence. If a fully ordered, gap-free sequence is required, consider using the ROW_NUMBER window function.
CREATE TABLE table_of_2_years_date_times AS
SELECT
date_time::date as date,
date_time::time as time
FROM (
SELECT
row_number() over (order by null)-1 as rn
,dateadd('minute', 15 * rn, '2022-03-01'::date) as date_time
from table(generator(rowcount=>4*24*365*2))
)
ORDER BY rn;
然后选择 top/bottom:
(SELECT * FROM table_of_2_years_date_times ORDER BY date,time LIMIT 5)
UNION ALL
(SELECT * FROM table_of_2_years_date_times ORDER BY date desc,time desc LIMIT 5)
ORDER BY 1,2;
DATE
TIME
2022-03-01
00:00:00
2022-03-01
00:15:00
2022-03-01
00:30:00
2022-03-01
00:45:00
2022-03-01
01:00:00
2024-02-28
22:45:00
2024-02-28
23:00:00
2024-02-28
23:15:00
2024-02-28
23:30:00
2024-02-28
23:45:00
我正在尝试以 15 分钟的间隔在 Snowflake 中创建一个 table。我试过使用发电机,但在 15 分钟的间隔内没有给出。有没有什么函数可以用来生成和构建这个 table 几年的有价值的数据。
比如
Date | Hour |
---|---|
202-03-29 | 02:00 AM |
202-03-29 | 02:15 AM |
202-03-29 | 02:30 AM |
202-03-29 | 02:45 AM |
202-03-29 | 03:00 AM |
202-03-29 | 03:15 AM |
......... | ........ |
......... | ........ |
谢谢
使用以下作为间隔 15 分钟的时间生成器,然后根据需要使用其他日期时间函数在单独的列中提取日期部分或时间部分。
with CTE as
(select timestampadd(min,seq4()*15 ,date_trunc(hour, current_timestamp())) as time_count
from table(generator(rowcount=>4*24)))
select time_count from cte;
+-------------------------------+
| TIME_COUNT |
|-------------------------------|
| 2022-03-29 14:00:00.000 -0700 |
| 2022-03-29 14:15:00.000 -0700 |
| 2022-03-29 14:30:00.000 -0700 |
| 2022-03-29 14:45:00.000 -0700 |
| 2022-03-29 15:00:00.000 -0700 |
| 2022-03-29 15:15:00.000 -0700 |
.
.
.
....truncated output
| 2022-03-30 13:15:00.000 -0700 |
| 2022-03-30 13:30:00.000 -0700 |
| 2022-03-30 13:45:00.000 -0700 |
+-------------------------------+
这个问题已经有很多答案
但要注意的主要一点是您不得使用 SEQx()
作为数字生成器(您可以在 ORDER BY 中使用它,但这不是必需的)。如 doc's
Important
This function uses sequences to produce a unique set of increasing integers, but does not necessarily produce a gap-free sequence. When operating on a large quantity of data, gaps can appear in a sequence. If a fully ordered, gap-free sequence is required, consider using the ROW_NUMBER window function.
CREATE TABLE table_of_2_years_date_times AS
SELECT
date_time::date as date,
date_time::time as time
FROM (
SELECT
row_number() over (order by null)-1 as rn
,dateadd('minute', 15 * rn, '2022-03-01'::date) as date_time
from table(generator(rowcount=>4*24*365*2))
)
ORDER BY rn;
然后选择 top/bottom:
(SELECT * FROM table_of_2_years_date_times ORDER BY date,time LIMIT 5)
UNION ALL
(SELECT * FROM table_of_2_years_date_times ORDER BY date desc,time desc LIMIT 5)
ORDER BY 1,2;
DATE | TIME |
---|---|
2022-03-01 | 00:00:00 |
2022-03-01 | 00:15:00 |
2022-03-01 | 00:30:00 |
2022-03-01 | 00:45:00 |
2022-03-01 | 01:00:00 |
2024-02-28 | 22:45:00 |
2024-02-28 | 23:00:00 |
2024-02-28 | 23:15:00 |
2024-02-28 | 23:30:00 |
2024-02-28 | 23:45:00 |