【发布时间】:2020-02-14 18:51:05
【问题描述】:
我想根据 AWS Redshift 中表中的列将表中的单行扩展到多行。
这是我的示例表架构和行:
CREATE TABLE test (
start timestamp, -- start time of the first slot
slot_length int, -- the length of the slots in minutes
repeat int -- how many slots will be there
);
INSERT INTO test (start, slot_length, repeat) VALUES
('2019-09-22T00:00:00', 90, 2),
('2019-09-21T15:30:00', 60, 3);
我想根据“重复”列的值将这两行扩展为 5。所以任何行都会被扩展“重复”次数。第一次扩展不会改变任何东西。后续扩展需要在“start”列中添加“slot_length”。这是我最后想要的行的最终列表:
'2019-09-22 00:00:00', 90, 2 -- expanded from the first row
'2019-09-22 01:30:00', 90, 2 -- expanded from the first row
'2019-09-21 15:30:00', 60, 3 -- expanded from the second row
'2019-09-21 16:30:00', 60, 3 -- expanded from the second row
'2019-09-21 17:30:00', 60, 3 -- expanded from the second row
这可以通过 Redshift 中的纯 SQL 来完成吗?
【问题讨论】:
-
可能类似:Convert one row into multiple rows with fewer columns 您对具有可变槽持续时间的要求使得在纯 SQL 中执行任务变得更加困难。 Stored Procedure 可能是一个很好的用例,或者只是在 Redshift 之外完成工作并导入结果。
-
我最终使用了 SQL 和 Python 代码的组合
标签: amazon-redshift