给定一个包含开始和结束日期列的表格,我正在尝试以时间序列的形式获取各个行条目,所有日期都在上述日期之间
我已经从这里尝试了一段代码,但是 https://community.periscopedata.com/t/18wkh8/generate-series-of-dates-in-snowflake
select dateadd(day, '-' || seq4(), current_date()) as dte
from table (generator(rowcount => (Difference between start and end date))
Input:
Start_date End_date Value
2019-01-01 2019-01-15 1$
2019-01-16 2019-01-23 2$
Output:
Date Value
2019-01-01 1$
2019-01-02 1$
---- so on
2019-01-05 1$
2019-01-16 2$
--- so on
2019-01-23 2$
rowcount => 需要是一个常量,但您可以使其足够大并在以后对其进行过滤,而不会花费明显的执行时间。 例如,如果 DTS 是包含日期范围的表的名称,则以下内容有效:
with
maxdiff as (
select max(datediff(day, start_date, end_date)) days
from dts),
cal as (
select seq4() n
from table(generator(rowcount => 10000))
)
select
dateadd(day, n, start_date) theDate,
value
from dts join cal
where n <= (select days+1 from maxdiff)
and theDate >= start_date and theDate <= end_date
order by theDate
;
斯图尔特的回答有一个问题:seq4()
不能保证给出连续的数字——你可以以一系列不同的日期结束。
相反,您可以执行以下操作:
select -1 + row_number() over(order by 0) i, start_date + i generated_date
from (select '2020-01-01'::date start_date, '2020-01-15'::date end_date)
join table(generator(rowcount => 10000 )) x
qualify i < 1 + end_date - start_date