ITPub博客

首页 > 数据库 > Oracle > 现在需要统计出差天数 (可能存在夸月的情况)

现在需要统计出差天数 (可能存在夸月的情况)

原创 Oracle 作者:xulongxc 时间:2014-06-24 17:52:00 0 删除 编辑
有4个字段  姓名(name)  出差日期(go_date) 回日期(end_date) , 出差天数(totalday)
假设有4条记录 
a   2014-6-20 2014-6-30  11
a    2014-5-28 2014-6-3   6
a    2014-6-28  2014-7-3   6
a    2014-5-29  2014-7-2   4
b   2014-5-29  2014-7-2    34


现在需要统计6月 出差天数 


如果去跟回都是在6月 很简单


with t as (

select 'a'name,date'2014-6-20'begin_date,date'2014-6-30' end_date, 11   days from dual union all
select 'a'name,date'2014-5-28'begin_date,date'2014-6-3'  end_date,6     days from dual union all
select 'a'name,date'2014-6-28'begin_date,date'2014-7-3'  end_date,6     days from dual union all
select 'a'name,date'2014-5-29'begin_date,date'2014-7-2'  end_date,4     days from dual union ALL
select 'a'name,date'2014-5-29'begin_date,date'2014-5-30'  end_date,1     days from dual union all
select 'b'name,date'2014-5-29'begin_date,date'2014-7-2'  end_date, 34   days from dual
)
select name,sum(case when (least(end_date,date'2014-06-30')-greatest(begin_date,date'2014-6-1')+1)<0 then 0 else least(end_date,date'2014-06-30')-greatest(begin_date,date'2014-6-1') +1 end )from t group by name;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29440247/viewspace-1192856/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: PL/SQL快捷键设置
请登录后发表评论 登录
全部评论

注册时间:2014-01-13

  • 博文量
    58
  • 访问量
    237555