如何使用 SQL 在 Teradata 中使用 table 的开始日期和结束日期创建日期列?
How can I create a column of dates using the start and end dates from a table in Teradata using SQL?
table 列是“Product_Number”、“Product_Name”、“Start_Date”、“End_Date”
Table 在 Teradata 中:
需要输出:
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
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
或 return 始终是每月 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
table 列是“Product_Number”、“Product_Name”、“Start_Date”、“End_Date”
Table 在 Teradata 中:
需要输出:
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
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
或 return 始终是每月 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