【问题标题】:How can I create a column of dates using the start and end dates from a table in Teradata using SQL?如何使用 SQL 使用 Teradata 中的表中的开始日期和结束日期创建日期列?
【发布时间】:2021-01-21 21:14:33
【问题描述】:

表格列是“Product_Number”、“Product_Name”、“Start_Date”、“End_Date”

Teradata 中的表:

需要输出:

【问题讨论】:

  • 输出是 Teradata 表还是 SAS 数据集?

标签: sql loops date sas teradata


【解决方案1】:

在 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

【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

    猜你喜欢
    • 2012-02-06
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    相关资源
    最近更新 更多