COGNOS - 不包括周末之间的天数

时间:2014-11-14 11:31:46

标签: cognos

我想计算开始日[开始]和结束日期之间的天数[完成];

我的当前代码工作正常,但包括周末,

_days_between (IF ([Finish] is missing) THEN (current_date) ELSE (cast([Finish] as date)),cast([Start] as date)) + 1

编辑(基于 Alexey Baturin 答案):

1 + 5 * (_days_between (cast([Finish] as date);cast([Start] as date)) 
     -_day_of_week (cast([Finish] as date);1)+_day_of_week (cast([Start] as date);1))/7 
- if (_day_of_week (cast([Start] as date);1) > 5) then (6) else  (_day_of_week (cast([Start] as date);1)) 
+ if (_day_of_week (cast([Finish] as date);1) > 5) then (5) else (_day_of_week (cast([Finish] as date);1))

我现在有了这个代码但是如果[Finish]缺失则不包括这个。

1 个答案:

答案 0 :(得分:2)

尝试

1 + (_week_of_year (IF ([Finish] is missing) THEN (current_date) ELSE (cast([Finish] as date))) - _week_of_year (cast([Start] as date))) * 5
- if (_day_of_week (cast([Start] as date),1) > 5) then (6) else  (_day_of_week (cast([Start] as date),1)) 
+ if (_day_of_week (IF ([Finish] is missing) THEN (current_date) ELSE (cast([Finish] as date)),1) > 5) then (5) else (_day_of_week (IF ([Finish] is missing) THEN (current_date) ELSE (cast([Finish] as date)),1))

或者更简单,使计算的想法变得清晰。

1 + (_week_of_year ([Finish]) - _week_of_year ([Start])) * 5
- if (_day_of_week ([Start];1) > 5) then (6) else  (_day_of_week ([Start];1)) 
+ if (_day_of_week ([Finish];1) > 5) then (5) else (_day_of_week ([Finish];1))

年终错误修复

1 + 5 * (_days_between ([Finish];[Start]) 
         -_day_of_week ([Finish];1)+_day_of_week ([Start];1))/7
- if (_day_of_week ([Start];1) > 5) then (6) else  (_day_of_week ([Start];1)) 
+ if (_day_of_week ([Finish];1) > 5) then (5) else (_day_of_week ([Finish];1))