Calculate 8 working days in past in Informix

只谈情不闲聊 提交于 2020-01-17 02:59:04

问题


I'm trying to write a script in C shell for selecting data from Informix database 8 working days in past. So far I have sql code that calculate 8 days in past + Sunday and Saturday it looks like this:

select *
from ekzo 
where datzah = today-
(case
        when weekday(today) = 1 then 12
        when weekday(today) = 2 then 12
        when weekday(today) = 3 then 12
        when weekday(today) = 4 then 10
        when weekday(today) = 5 then 10
        when weekday(today) = 6 then 10
        when weekday(today) = 0 then 11
        end)

I have created table "prazkal" with holidays that looks like this:

datpra  01.01.2014
nazpra  Nova Godina
krapra  SRI

datpra  06.01.2014
nazpra  Bogojavljanje ili Sveta tri kralja
krapra  PON

datpra  20.04.2014
nazpra  Uskrs
krapra  NED

datpra  21.04.2014
nazpra  Uskršnji ponedjeljak
krapra  PON

...

I don't know how to extend my sql to calculate 8 working days in past, considering weekends and holidays.


回答1:


I would do it in 2 functions. First function checks if day is a holiday:

create function is_holiday(d datetime year to day)
returning boolean;
    -- define hcnt integer;

    if weekday(d) = 0 or weekday(d) = 6 then
        return 't';
    end if;

    -- code that check if 'd' is marked as holiday in calendar
    --select count(*) into hcnt from prazkal where datpra = d;
    --if hcnt > 0 then
    --  return 't';
    --end if;

    return 'f';
end function;

Second function decreases date by some days omitting holidays:

create function move_date_back(start_d datetime year to day, count_days integer)
returning datetime year to day;
define new_d datetime year to day;
define i integer;
    let i = 0;
    let new_d = start_d;

    while i < count_days
        let new_d = new_d - interval(1) day to day;
        if not is_holiday(new_d) then
            let i = i + 1;
        end if;
    end while;

    return new_d;
end function;


来源:https://stackoverflow.com/questions/24693287/calculate-8-working-days-in-past-in-informix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!