如何在没有星期六和星期日的情况下在db2中获得WORKING日差异?

时间:2011-11-15 10:44:26

标签: sql db2

我的桌子上有一些日期间隔如下:

|id|group|date_start|date_end|

我想提出一个看起来像的视图:

|id|working_days_diff|

|id|group|working_days_diff|

我该怎么办? 感谢

我正在考虑一个自定义函数,如果DAYOFWEEK不是星期六和星期日,它会在每天的差异和总和中循环...但我不知道如何制作......

7 个答案:

答案 0 :(得分:1)

db2有一个名为TIMESTAMPDIFF而不是DATEDIFF的函数。用它来查找日期之间的周数,然后乘以5(工作日)。

 TIMESTAMPDIFF(32,CHAR(TIMESTAMP('2001-09-29-11.25.42.123456') - TIMESTAMP('2001-09-26-12.07.58.123456'))) 

注意:该函数是估计值

答案 1 :(得分:1)

CREATE FUNCTION stkqry.WORKING_DAY_DIFF(DATE_START date, DATE_END date)          
RETURNS INTEGER                                            
LANGUAGE SQL
BEGIN
    DECLARE WORKING_DAYS INTEGER DEFAULT 0;
    DECLARE DATE_COUNTER DATE;
    SET DATE_COUNTER = DATE_START;
    WHILE DAYS(DATE_COUNTER) < DAYS(DATE_END) DO
        SET DATE_COUNTER = DATE(days(DATE_COUNTER)+1);

        CASE WHEN DAYOFWEEK_ISO(DATE_COUNTER) = 6 THEN
            SET WORKING_DAYS = WORKING_DAYS;
        WHEN DAYOFWEEK_ISO(DATE_COUNTER) = 7 THEN
            SET WORKING_DAYS = WORKING_DAYS;
        ELSE
            SET WORKING_DAYS = WORKING_DAYS + 1;
        END CASE;
    END WHILE;
    RETURN WORKING_DAYS;
END

答案 2 :(得分:1)

日历表使这种查询变得容易(这是SQL Server特定的链接):

http://web.archive.org/web/20070611150639/http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html

但是,您可以为任何数据库构建这些;下面是一个建议如何为db2做的链接:

http://bytes.com/topic/db2/answers/181183-calculating-business-days

答案 3 :(得分:1)

select 
TIMESTAMPDIFF(32,CHAR(
    TIMESTAMP(
        (CHAR(date('2013-09-19') - (DAYOFWEEK_ISO('2013-09-19')-1) DAYS ))||'-00.00.00.000000'
    ) 
    - TIMESTAMP(
        (CHAR(date('2013-09-13') - (DAYOFWEEK_ISO('2013-09-13')-1) DAYS )||'-00.00.00.000000')
    )
))*5 + DAYOFWEEK_ISO('2013-09-19')- DAYOFWEEK_ISO('2013-09-13') 

答案 4 :(得分:1)

取自Count work days between two dates

(days(PAID_DATE) - days(SUBMISSION_DATE) + 1) -
((52 * (year(PAID_DATE) - year(SUBMISSION_DATE)) + week(PAID_DATE) - week(SUBMISSION_DATE)) * 2) - 
(case when dayofweek(SUBMISSION_DATE) = 1 then 1 else 0 end) -
(case when dayofweek(PAID_DATE) = 7 then 1 else 0 end)
as SUBMISSION_TO_PAID

答案 5 :(得分:0)

如果“ 2020/09/28”是结束日期,而“ 2020/09/14”是开始日期:

5 * ((DAYS('2020-09-28') -  DAYS('2020-09-14')) / 7) + SUBSTRING('0123444401233334012222340111123400012345001234550', 7 * (DAYOFWEEK_ISO('2020-09-14')-1)
+ DAYOFWEEK_ISO('2020-09-28'), 1)

(从mysql中的函数转换为db2)

答案 6 :(得分:0)

以下内容对我来说很有效:

-- TABLE WITH ALL DATES (FOR TESTING ONLY)
CREATE TABLE CALENDAR(THEDAY DATE)
-- INSERT INTO CALENDAR VALUES ( CURRENT_DATE - x DAY);
-- INSERT AS MANY DATES YOU WANT FOR TESTING
SELECT * FROM CALENDAR ORDER BY THEDAY DESC

-- TABLE OF HOLIDAY

CREATE TABLE CAL_HOLIDAY(HOLDAY DATE);
DELETE FROM CAL_HOLIDAY
INSERT INTO CAL_HOLIDAY VALUES(DATE('2020-11-11')) -- NOV 11:HOLIDAY IN FRANCE
INSERT INTO CAL_HOLIDAY VALUES(DATE('2020-08-15')) -- AUG 15:HOLIDAY IN FRANCE
SELECT * FROM CAL_HOLIDAY

设置好舞台后

-- SELECT DATES out of our test table and THE DIFF (with today here)

SELECT THEDAY,
-- DIFFERENCE BETWEEN DATES
DAYS(CURRENT_DATE) - DAYS(THEDAY) 
-- PLACE WEEK ENDS CORRECTLY AND REMOVE AS MANY W-E
- FLOOR((DAYS(CURRENT_DATE) - DAYS(THEDAY) + 8 - DAYOFWEEK(CURRENT_DATE))/7)*2 
-- CORRECT SUNDAY OFFSET
+ FLOOR((7-DAYOFWEEK(THEDAY))/6)
 -- REMOVE NUMBER OF HOLIDAY (if they are not in the W-E)
-(SELECT COUNT(*) FROM CAL_HOLIDAY WHERE 
    HOLDAY BETWEEN THEDAY AND CURRENT_DATE AND 
    DAYOFWEEK(HOLDAY) NOT IN (1,7))
FROM CALENDAR ORDER BY THEDAY DESC