如何在teradata中将列A(日期列)添加到B列(工作日数)以获取新日期?

时间:2015-02-22 01:53:19

标签: loops date teradata procedure

这是我的数据;

表A.pickup_date是一个日期列

表A.biz_days是我想要添加到A.pickup_date的工作日

表B.date

表B.is_weekend(Y或N)

表B. is_holiday(Y或N)

基本上从表B中我知道每个日期,是否有任何日期是工作日。现在,我希望在将A.business_days添加到A.pickup_date后的确切日期,在表A中有第三列。

任何人都可以向我提供声明或程序陈述的案例吗?不幸的是,我们不允许在Teradata中编写自己的函数。

2 个答案:

答案 0 :(得分:0)

这很丑陋,但我认为它应该让你开始。

首先,我创建了一个表示表a的易失性表:

CREATE VOLATILE TABLE vt_pickup AS
(SELECT CURRENT_DATE AS pickup_date,
8 AS Biz_Days) WITH DATA PRIMARY INDEX(pickup_date)
ON COMMIT PRESERVE ROWS;

INSERT INTO vt_pickup VALUES ('2015-02-24',5);

然后我加入sys_calendar.calendar来获取一周的日子:

CREATE VOLATILE TABLE VT_Days AS 
(
SELECT
            p.pickup_date,
            day_of_week
            FROM

            vt_pickup p
            INNER JOIN sys_calendar.CALENDAR c
            ON c.calendar_date >= p.pickup_date 
            AND c.calendar_date < (p.pickup_date + Biz_Days)
) WITH DATA 
PRIMARY INDEX(pickup_date)
ON COMMIT PRESERVE ROWS

然后我可以使用所有这些来生成实际的交付日期:

SELECT 
p.pickup_date,
p.biz_days,
biz_days + COUNT(sundays.day_of_week) + COUNT (saturdays.day_of_week) AS TotalDays,
COUNT (sundays.day_of_week) AS Suns,
COUNT (saturdays.day_of_week) AS Sats,
p.pickup_date + totaldays AS Delivery_Date,
FROM 
    vt_pickup p
    LEFT JOIN vt_days AS Sundays ON
         p.pickup_date = sundays.pickup_date
         AND sundays.day_of_week = 1
        LEFT JOIN vt_days AS saturdays ON
            p.pickup_date = saturdays.pickup_date
            AND saturdays.day_of_week = 7
GROUP BY 1,2

你应该可以在假期中使用带有其他别名的逻辑。

答案 1 :(得分:0)

最简单的方法是计算一个工作日的连续数量(如果它是一个重复的操作,则将其添加到日历表中的新列,否则使用WITH):

SUM(CASE WHEN is_weekend = 'Y' OR is_holiday = 'Y' THEN 0 ELSE 1 END)
OVER (ORDER BY calendar_date
      ROWS UNBOUNDED PRECEDING) AS biz_day#

然后你需要两个连接:

SELECT ..., c2.calendar_date 
FROM tableA AS a 
JOIN tableB AS c1
  ON a.pickup_date = c1.calendar_date
JOIN tableB AS c2
  ON c2.biz_day# = c1.biz_day# + a.biz_days 
 AND is_weekend = 'N'
 AND is_holiday = 'N'