在列表中查找下一个匹配值

时间:2012-05-21 17:48:58

标签: teradata

只有Teradata SQL可供使用。

我正在尝试动态创建一个可以运行作业的日期列表。这是我正在处理的简化版本。

Calendar (covers 2012+)
--------
Calender Date
Business Day Flag - 1 = This is a Business Day, 0 = Weekend
Holiday Flag - 1 = This is a Holiday, 0 = Non-Holiday
Year
Month
Day

Schedule (several more fields available, but not relevant)
--------
Day Type - Business Day or Calendar Date
Day Number - Represents either a Business or Calendar Day
Eligible to run on holiday - Yes / No

通过选择

中的案例陈述完成逻辑
when d.day_type = 'BD' and day_num = c.business_day_of_month  then 'Y'
when d.day_type = 'CD' and day_num = c.day_of_month then 'Y'
else 'N' as run_flag

但是,当run_flag ='Y'和Holiday_Flag = 1时,应列出作业以在下一个商业/日历日运行(取决于day_type)。

我认为我需要使用超前/滞后,但我不知道是否可以让它贯穿所涉及的逻辑。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

select
    a.calendar_dt,
    min(b.dt) as next_business_dt
from my_calendar a
cross join my_calendar b
where a.calendar_dt < b.calendar_dt
and b.business_day_flag = 1
and b.holiday_flag = 0
group by
    a.calendar_dt