将一行复制一定次数,然后每次将 30 分钟添加到时间戳(如序列)
Duplicating a row a certain number of times and then adding 30 mins to a timestamp each time (like a sequence)
示例数据:
ID Location Type Trip End Number of periods
1298 Residential 02/01/2022 05:30 48
1298 Residential 03/01/2022 05:30 6
1244 Commercial 31/12/2021 09:00 2
1244 Residential 31/12/2021 10:30 1
基本上,我想根据 'number of periods.' 列中的数字复制该行。另外,为每个新的复制行添加 30 分钟。
所以新的第二行是:
1298 Residential 02/01/2022 06:00 48
所以这在雪花中使用 table generator (which have a fixed input) combined with ROW_NUMBER and then using DATEADD
解决了
我更改了 48 -> 8 以使输出不那么难看。
WITH data(ID, LocationType, TripEnd, NumPeriods) as (
select COLUMN1, COLUMN2, TO_TIMESTAMP_NTZ(COLUMN3, 'dd/mm/yyyy hh:mi'), COLUMN4 from values
(1298, 'Residential','02/01/2022 05:30',8),
(1298, 'Residential','03/01/2022 05:30',6),
(1244, 'Commercial','31/12/2021 09:00',2),
(1244, 'Residential','31/12/2021 10:30',1)
), set_of_nums as (
SELECT row_number() over (order by null)-1 as rn
FROM table(generator(ROWCOUNT => 1000))
)
select d.*
,dateadd(minute, 30 * s.rn, TripEnd) as range_time
FROM DATA as d
JOIN set_of_nums as s ON d.NumPeriods >= s.rn
ORDER BY 1,3,5
;
给出:
ID
LOCATIONTYPE
TRIPEND
NUMPERIODS
RANGE_TIME
1244
Commercial
2021-12-31 09:00:00.000
2
2021-12-31 09:00:00.000
1244
Commercial
2021-12-31 09:00:00.000
2
2021-12-31 09:30:00.000
1244
Commercial
2021-12-31 09:00:00.000
2
2021-12-31 10:00:00.000
1244
Residential
2021-12-31 10:30:00.000
1
2021-12-31 10:30:00.000
1244
Residential
2021-12-31 10:30:00.000
1
2021-12-31 11:00:00.000
1298
Residential
2022-01-02 05:30:00.000
8
2022-01-02 05:30:00.000
1298
Residential
2022-01-02 05:30:00.000
8
2022-01-02 06:00:00.000
1298
Residential
2022-01-02 05:30:00.000
8
2022-01-02 06:30:00.000
1298
Residential
2022-01-02 05:30:00.000
8
2022-01-02 07:00:00.000
1298
Residential
2022-01-02 05:30:00.000
8
2022-01-02 07:30:00.000
1298
Residential
2022-01-02 05:30:00.000
8
2022-01-02 08:00:00.000
1298
Residential
2022-01-02 05:30:00.000
8
2022-01-02 08:30:00.000
1298
Residential
2022-01-02 05:30:00.000
8
2022-01-02 09:00:00.000
1298
Residential
2022-01-02 05:30:00.000
8
2022-01-02 09:30:00.000
1298
Residential
2022-01-03 05:30:00.000
6
2022-01-03 05:30:00.000
1298
Residential
2022-01-03 05:30:00.000
6
2022-01-03 06:00:00.000
1298
Residential
2022-01-03 05:30:00.000
6
2022-01-03 06:30:00.000
1298
Residential
2022-01-03 05:30:00.000
6
2022-01-03 07:00:00.000
1298
Residential
2022-01-03 05:30:00.000
6
2022-01-03 07:30:00.000
1298
Residential
2022-01-03 05:30:00.000
6
2022-01-03 08:00:00.000
1298
Residential
2022-01-03 05:30:00.000
6
2022-01-03 08:30:00.000
示例数据:
ID Location Type Trip End Number of periods
1298 Residential 02/01/2022 05:30 48
1298 Residential 03/01/2022 05:30 6
1244 Commercial 31/12/2021 09:00 2
1244 Residential 31/12/2021 10:30 1
基本上,我想根据 'number of periods.' 列中的数字复制该行。另外,为每个新的复制行添加 30 分钟。 所以新的第二行是:
1298 Residential 02/01/2022 06:00 48
所以这在雪花中使用 table generator (which have a fixed input) combined with ROW_NUMBER and then using DATEADD
解决了我更改了 48 -> 8 以使输出不那么难看。
WITH data(ID, LocationType, TripEnd, NumPeriods) as (
select COLUMN1, COLUMN2, TO_TIMESTAMP_NTZ(COLUMN3, 'dd/mm/yyyy hh:mi'), COLUMN4 from values
(1298, 'Residential','02/01/2022 05:30',8),
(1298, 'Residential','03/01/2022 05:30',6),
(1244, 'Commercial','31/12/2021 09:00',2),
(1244, 'Residential','31/12/2021 10:30',1)
), set_of_nums as (
SELECT row_number() over (order by null)-1 as rn
FROM table(generator(ROWCOUNT => 1000))
)
select d.*
,dateadd(minute, 30 * s.rn, TripEnd) as range_time
FROM DATA as d
JOIN set_of_nums as s ON d.NumPeriods >= s.rn
ORDER BY 1,3,5
;
给出:
ID | LOCATIONTYPE | TRIPEND | NUMPERIODS | RANGE_TIME |
---|---|---|---|---|
1244 | Commercial | 2021-12-31 09:00:00.000 | 2 | 2021-12-31 09:00:00.000 |
1244 | Commercial | 2021-12-31 09:00:00.000 | 2 | 2021-12-31 09:30:00.000 |
1244 | Commercial | 2021-12-31 09:00:00.000 | 2 | 2021-12-31 10:00:00.000 |
1244 | Residential | 2021-12-31 10:30:00.000 | 1 | 2021-12-31 10:30:00.000 |
1244 | Residential | 2021-12-31 10:30:00.000 | 1 | 2021-12-31 11:00:00.000 |
1298 | Residential | 2022-01-02 05:30:00.000 | 8 | 2022-01-02 05:30:00.000 |
1298 | Residential | 2022-01-02 05:30:00.000 | 8 | 2022-01-02 06:00:00.000 |
1298 | Residential | 2022-01-02 05:30:00.000 | 8 | 2022-01-02 06:30:00.000 |
1298 | Residential | 2022-01-02 05:30:00.000 | 8 | 2022-01-02 07:00:00.000 |
1298 | Residential | 2022-01-02 05:30:00.000 | 8 | 2022-01-02 07:30:00.000 |
1298 | Residential | 2022-01-02 05:30:00.000 | 8 | 2022-01-02 08:00:00.000 |
1298 | Residential | 2022-01-02 05:30:00.000 | 8 | 2022-01-02 08:30:00.000 |
1298 | Residential | 2022-01-02 05:30:00.000 | 8 | 2022-01-02 09:00:00.000 |
1298 | Residential | 2022-01-02 05:30:00.000 | 8 | 2022-01-02 09:30:00.000 |
1298 | Residential | 2022-01-03 05:30:00.000 | 6 | 2022-01-03 05:30:00.000 |
1298 | Residential | 2022-01-03 05:30:00.000 | 6 | 2022-01-03 06:00:00.000 |
1298 | Residential | 2022-01-03 05:30:00.000 | 6 | 2022-01-03 06:30:00.000 |
1298 | Residential | 2022-01-03 05:30:00.000 | 6 | 2022-01-03 07:00:00.000 |
1298 | Residential | 2022-01-03 05:30:00.000 | 6 | 2022-01-03 07:30:00.000 |
1298 | Residential | 2022-01-03 05:30:00.000 | 6 | 2022-01-03 08:00:00.000 |
1298 | Residential | 2022-01-03 05:30:00.000 | 6 | 2022-01-03 08:30:00.000 |