诚心请教 关于请教一个时间统计的查询语句

luca623 2014-06-21 03:45:32
有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月 很简单


select name,sum(totalday) from statics where to_char(go_date,'MM')='06' and to_char(end_date)='06' group by name 就搞定了
像第2条 去是5月28号 回是6月3号 应该取的天数就是3天
像第3条 回是7月3号 去是6月6月28号 应该取的也是3天
第4条 去是5月29号 回是7月2号 这种 就应该是30天(假设6月是30天的情况)
这种应该怎么写呢 本人学艺不精 请个人指教啊 谢谢
...全文
284 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
crusher395 2014-06-24
  • 打赏
  • 举报
回复
用decode构造一个三值运算把小于/大于 6.1/6.30的天移到6.1/6.30即可,
select sum(
	decode(sign(to_date('30-jun-14','dd-mm-yy')-end_Date),1,end_date, '30-jun-14')
	-
	decode(sign(go_date-to_date('1-jun-14','dd-mm-yy')),1,go_date, '1-jun-14')
	+1
	)
from statistics
where end_Date>='1-jun-14' and go_date<='30-jun-14' and name='a';
结果验证正确
luca623 2014-06-22
  • 打赏
  • 举报
回复
谢谢两位的回答 我先看看 学习学习 搞定 了 马上结分
CT_LXL 2014-06-21
  • 打赏
  • 举报
回复
引用 楼主 luca623 的回复:
有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月 很简单 select name,sum(totalday) from statics where to_char(go_date,'MM')='06' and to_char(end_date)='06' group by name 就搞定了 像第2条 去是5月28号 回是6月3号 应该取的天数就是3天 像第3条 回是7月3号 去是6月6月28号 应该取的也是3天 第4条 去是5月29号 回是7月2号 这种 就应该是30天(假设6月是30天的情况) 这种应该怎么写呢 本人学艺不精 请个人指教啊 谢谢

with t as
 (select 'a' name,
         to_date('2014-06-20', 'yyyy-mm-dd') go_date,
         to_date('2014-06-30', 'yyyy-mm-dd') end_date
    from dual
  union all
  select 'a' name,
         to_date('2014-05-28', 'yyyy-mm-dd') go_date,
         to_date('2014-06-03', 'yyyy-mm-dd') end_date
    from dual
  union all
  select 'a' name,
         to_date('2014-06-28', 'yyyy-mm-dd') go_date,
         to_date('2014-06-30', 'yyyy-mm-dd') end_date
    from dual
  union all
  select 'a' name,
         to_date('2014-05-29', 'yyyy-mm-dd') go_date,
         to_date('2014-06-30', 'yyyy-mm-dd') end_date
    from dual
  union all
  select 'b' name,
         to_date('2014-05-29', 'yyyy-mm-dd') go_date,
         to_date('2014-07-02', 'yyyy-mm-dd') end_date
    from dual)
select name, count(*)
  from (select name, go_date + level - 1 dt, rn
          from (select t.*, rownum rn from t)
        connect by go_date + level <= end_date + 1
               and prior rn = rn
               and prior dbms_random.value is not null)
 where to_char(dt, 'mm') = '06'
 group by rn, name;

wqkjj 2014-06-21
  • 打赏
  • 举报
回复
decode(to_char(end_date,'MM')-to_char(go_date,'MM'), 0, trunc(end_date)-trunc(go_date)+1, -- 当月 1, trunc(last_day(go_date))-trunc(go_date)+1, -- 上月 trunc(last_day(go_date))-trunc(to_date(to_char(go_date,'YYYYMM'),'YYYYMM')-1)) --上上,上上上....月

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧