有两个表:表1与表2【表1】,制造实际时间
字段A:
20121031063702
年:4位
月:2位
日:2位
小时:2位
分钟: 2位
秒:2位字段B:
20121031120031
格式与A一样【表2】,工作时间表
字段C:
20121031000000
格式与【表1】A字段一样。字段D:
0121031060000
格式与【表1】A一样字段E:
W:表示该条纪录C到D的时间为工作时间
R:表示该条纪录C到D时间为休息时间表2的纪录(10月31号当天的)C                   D                   E
----------------------------------------------
20121031000000    20121031010000        W
20121031010000    20121031060000        R
20121031060000    20121031150000        W
20121031150000    20121031240000        W
求:【表1】字段A与字段B的间隔小时数,该小时数需要去除【表2】中该天的休息时间小时数!两个表时间上有交叉哦。相当有挑战,不知道哪个大侠能接招。

解决方案 »

  1.   

    我写了一个:
    select (to_date(t1.b, 'YYYYMMDDHH24MISS') - to_date(t1.a, 'YYYYMMDDHH24MISS')) * 24 /
           (select sum(to_date(t2.d, 'YYYYMMDDHH24MISS') - to_date(t2.c, 'YYYYMMDDHH24MISS')) * 24
              from t2
             where t2.c like substr(t1.a, 1, 8) || '%'
               and t2.d like substr(t1.a, 1, 8) || '%'
               and t2.e = 'R')
      from t1
      

  2.   

    select 
           (trunc(to_date('20121031120031','yyyymmddhh24miss'),'hh')-trunc(to_date('20121031063702','yyyymmddhh24miss'),'hh'))*24 -b表的小时数的算法不清楚
    from a,b;
    where substr(a.a,0,8) = substr(b.c,0,8)