【发布时间】:2021-01-21 21:14:33
【问题描述】:
【问题讨论】:
-
输出是 Teradata 表还是 SAS 数据集?
标签: sql loops date sas teradata
【问题讨论】:
标签: sql loops date sas teradata
在 Teradata 中有专有语法 expand on 可以使用句点创建时间序列:
select Product_Number, Product_Name
-- extract the start date of the period value
, begin(pd) as new_Date
from tab
-- create a period on the fly and return one row per day
-- periods include the start, but exclude the end, thus end_date+1
expand on period(start_date, end_date+1) as pd
假设您的示例日期在mm-dd-yyyy format,如果是dd-mm-yyyy,则需要按月扩展:
select Product_Number, Product_Name, begin(pd) as new_Date
from tab
expand on period(start_date, end_date+1) as pd by interval '1' month
或者总是返回一个月的 1 号:
select Product_Number, Product_Name, begin(pd) as new_Date
from tab
expand on period(start_date, end_date+1) as pd by anchor period month_begin
【讨论】:
with cte as (
SELECT [PRoduct_number]
,[Product_name]
,[Start_date]
,[End_date]
FROM table_name
union all
select
[PRoduct_number]
,[Product_name]
,ADD_MONTHS(cte.[Start_date], 1 )
,[End_date]
from
cte
where StartDate < End_Date
)
select * from cte
【讨论】:
sql标签不代表sql server或t-sql