Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
967 views
in Technique[技术] by (71.8m points)

sql - Calculate 8 working days in past in Informix

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...