使用日期序列交叉连接 Redshift
Cross join Redshift with sequence of dates
我想使用 redshift 执行以下 athena 查询,但到目前为止无法重现它。应该在 CREATE TABLE AS ()
语句中调用查询,因此 generate_sequence() 的想法可能行不通。有什么想法吗?
雅典娜查询:
SELECT
*
FROM table_one t1
CROSS JOIN UNNEST(slice(sequence(t1.effective_date, t1.expiration_date, INTERVAL '1' MONTH), 1 ,12)) AS t (sequence_date)
根据要求,我添加了一个示例来说明我正在尝试做什么。基本上我有一个有效间隔的记录(年份单位 1、2、3 ...),我想将它复制 N 次,以便将每个新记录分配到日期 YYYY-MM-DD + interval*12 /N 个月(见例子)
原始记录:
Date
variables
2021-05-06
values
待定(N=12,间隔1年)
Date
variables
2021-05-06
values/12
2021-06-06
values/12
2021-07-06
values/12
2021-08-06
values/12
2021-08-06
values/12
2021-10-06
values/12
2021-11-06
values/12
2021-12-06
values/12
2022-01-06
values/12
2022-02-06
values/12
2022-03-06
values/12
2022-04-06
values/12
待定(N=4,间隔两年)
Date
variables
2021-05-06
values/2
2021-11-06
values/2
2022-05-06
values/2
2022-11-06
values/2
感谢帮助
可能最好的方法是使用递归 CTE - https://docs.aws.amazon.com/redshift/latest/dg/r_WITH_clause.html
示例 - Generate rows with incrementing dates based on just a starting date in Redshift
你做的似乎比这个例子复杂一点。如果您无法获取 post 此处的专家可以使用一些示例数据为您创建示例查询。
============================================= ===
根据新信息和上述递归 CTE 过程,我想到了这个:
drop table if exists table_one ;
create table table_one (
dt date,
info varchar(32),
n int,
y int);
insert into table_one values ('2021-05-06', 'record info here', 12, 1);
commit;
with recursive dates(dt, info, n, y, end_dt) as
( select dt::date, info, n, y, dateadd(year, y, dt)::date as end_dt
from table_one
union all
select dateadd(months, 12 * y / n, dt)::date as dt, info, n, y, end_dt::date
from dates d
where d.dt < dateadd(month, -12 * y /n, end_dt)
)
select dt, info from dates;
我不确定这是否是您希望将 N 和年份纳入流程的方式,但希望您可以从此处进行修改。只需在 table_one 插入语句中更改 N 和年份的值,然后重新运行整个过程即可获得第二个结果。
我想使用 redshift 执行以下 athena 查询,但到目前为止无法重现它。应该在 CREATE TABLE AS ()
语句中调用查询,因此 generate_sequence() 的想法可能行不通。有什么想法吗?
雅典娜查询:
SELECT
*
FROM table_one t1
CROSS JOIN UNNEST(slice(sequence(t1.effective_date, t1.expiration_date, INTERVAL '1' MONTH), 1 ,12)) AS t (sequence_date)
根据要求,我添加了一个示例来说明我正在尝试做什么。基本上我有一个有效间隔的记录(年份单位 1、2、3 ...),我想将它复制 N 次,以便将每个新记录分配到日期 YYYY-MM-DD + interval*12 /N 个月(见例子)
原始记录:
Date | variables |
---|---|
2021-05-06 | values |
待定(N=12,间隔1年)
Date | variables |
---|---|
2021-05-06 | values/12 |
2021-06-06 | values/12 |
2021-07-06 | values/12 |
2021-08-06 | values/12 |
2021-08-06 | values/12 |
2021-10-06 | values/12 |
2021-11-06 | values/12 |
2021-12-06 | values/12 |
2022-01-06 | values/12 |
2022-02-06 | values/12 |
2022-03-06 | values/12 |
2022-04-06 | values/12 |
待定(N=4,间隔两年)
Date | variables |
---|---|
2021-05-06 | values/2 |
2021-11-06 | values/2 |
2022-05-06 | values/2 |
2022-11-06 | values/2 |
感谢帮助
可能最好的方法是使用递归 CTE - https://docs.aws.amazon.com/redshift/latest/dg/r_WITH_clause.html
示例 - Generate rows with incrementing dates based on just a starting date in Redshift
你做的似乎比这个例子复杂一点。如果您无法获取 post 此处的专家可以使用一些示例数据为您创建示例查询。
============================================= ===
根据新信息和上述递归 CTE 过程,我想到了这个:
drop table if exists table_one ;
create table table_one (
dt date,
info varchar(32),
n int,
y int);
insert into table_one values ('2021-05-06', 'record info here', 12, 1);
commit;
with recursive dates(dt, info, n, y, end_dt) as
( select dt::date, info, n, y, dateadd(year, y, dt)::date as end_dt
from table_one
union all
select dateadd(months, 12 * y / n, dt)::date as dt, info, n, y, end_dt::date
from dates d
where d.dt < dateadd(month, -12 * y /n, end_dt)
)
select dt, info from dates;
我不确定这是否是您希望将 N 和年份纳入流程的方式,但希望您可以从此处进行修改。只需在 table_one 插入语句中更改 N 和年份的值,然后重新运行整个过程即可获得第二个结果。