表A字段1为自增主键
字段2为车辆ID(可重复)
字段3为车辆工作工作小时
车辆4为车辆开始工作时间
车辆5为车辆结束工作时间要求1:
给出一个车辆ID(比如001)和日期范围(比如2012-8-1到2012-8-10),统计这辆车在每天的工作小时数,最终的效果的
  
车辆ID   车辆工作小时      日期
001        2小时         2012-8-1
001        3小时         2012-8-2
001        1小时         2012-8-3
...

解决方案 »

  1.   

    字段3为车辆工作工作小时车辆4为车辆开始工作时间
    车辆5为车辆结束工作时间这两个你不认为字段三是不需要的吗?还是你在trigger中处理,将结束减开始就是车辆工作工作小时统计时按哪个?
      

  2.   

    select sum(车辆工作工作小时)/(to_date('2012-8-10')-to_date('2012-8-1'
    )) from 表A where id='001' and 日期范围 between '2012-8-1'
    and '2012-8-10'
      

  3.   


    create sequence a_squ;
    create table a(
    id int primary key,
    cid int,
    starttime date,
    endtime date);insert into a values(a_squ.nextval,1,to_date('2012-03-04 08:03:04','yyyy-mm-dd hh24:mi:ss'),to_date('2012-03-04 10:05:04','yyyy-mm-dd hh24:mi:ss'));
    insert into a values(a_squ.nextval,1,to_date('2012-03-04 12:03:04','yyyy-mm-dd hh24:mi:ss'),to_date('2012-03-04 15:03:07','yyyy-mm-dd hh24:mi:ss'));
    insert into a values(a_squ.nextval,1,to_date('2012-03-05 12:03:04','yyyy-mm-dd hh24:mi:ss'),to_date('2012-03-05 18:03:04','yyyy-mm-dd hh24:mi:ss'));
    insert into a values(a_squ.nextval,2,to_date('2012-03-04 12:03:04','yyyy-mm-dd hh24:mi:ss'),to_date('2012-03-04 13:03:04','yyyy-mm-dd hh24:mi:ss'));
    insert into a values(a_squ.nextval,2,to_date('2012-03-05 12:03:05','yyyy-mm-dd hh24:mi:ss'),to_date('2012-03-05 17:03:04','yyyy-mm-dd hh24:mi:ss'));select 
        cid 车辆ID,
        round(sum(endtime-starttime)*24) 车辆工作小时,
        to_char(a.starttime,'yyyy-mm-dd') 日期
    from a
    where cid=1--id参数
    and to_char(a.starttime,'yyyy-mm-dd') between '2012-03-01' and '2012-03-31'--两个日期参数
    group by cid,to_char(a.starttime,'yyyy-mm-dd');
      

  4.   

    车联网的?
    同行啊,程序员OR DBA?哪个公司的?select 车辆ID,trunc(开始工作时间),sum(工作小时)
    from 表
    where 开始工作时间 between 2012-8-1 到 2012-8-10
    group by 车辆ID,trunc(开始工作时间)
      

  5.   

    查询当前月的所有天数
     select * from 
     (
     select (trunc(sysdate,'mm')+level-1) as dt  from dual
     connect by rownum<=35
     )
     where trunc(sysdate,'mm')=trunc(dt,'mm');
      

  6.   

    --思路:自增主键,一般用到序列(如果主键不是显式插入,可以通过后台用触发器实现插入),这里就显式插入,创建序列,简单的默认序列。
    create sequence a_squ;
    create table a(
    id int primary key,
    cid int,
    starttime date,
    endtime date);
    ---插入数据
    insert into a values(a_squ.nextval,1,to_date('2012-03-04 08:03:04','yyyy-mm-dd hh24:mi:ss'),to_date('2012-03-04 10:05:04','yyyy-mm-dd hh24:mi:ss'));
    insert into a values(a_squ.nextval,1,to_date('2012-03-04 12:03:04','yyyy-mm-dd hh24:mi:ss'),to_date('2012-03-04 15:03:07','yyyy-mm-dd hh24:mi:ss'));
    insert into a values(a_squ.nextval,1,to_date('2012-03-05 12:03:04','yyyy-mm-dd hh24:mi:ss'),to_date('2012-03-05 18:03:04','yyyy-mm-dd hh24:mi:ss'));
    insert into a values(a_squ.nextval,2,to_date('2012-03-04 12:03:04','yyyy-mm-dd hh24:mi:ss'),to_date('2012-03-04 13:03:04','yyyy-mm-dd hh24:mi:ss'));
    insert into a values(a_squ.nextval,2,to_date('2012-03-05 12:03:05','yyyy-mm-dd hh24:mi:ss'),to_date('2012-03-05 17:03:04','yyyy-mm-dd hh24:mi:ss'));---1、看结果有三个字段需要显示,车辆id直接呈现,日期,小时通过计算得到。--每天(每,各等关键字,使用group by),车辆工作小时=结束时间-开始时间
    SELECT a.cid 车辆id,sum((a.endtime-a.starttime)*24) 车辆工作小时,--*24是因为带时分秒的日期计算结果单位是天,要得到小时就乘以24
     trunc(a.starttime) 日期
    FROM a
    WHERE (这里填写日期统计的日期范围)
    GROUP BY trunc(a.starttime),a.cid --trunc函数得到无时无分无秒的日期
      

  7.   


    --思路:自增主键,一般用到序列(如果主键不是显式插入,可以通过后台用触发器实现插入),这里就显式插入,创建序列,简单的默认序列。
    create sequence a_squ;
    create table a(
    id int primary key,
    cid int,
    starttime date,
    endtime date);
    ---插入数据
    insert into a values(a_squ.nextval,1,to_date('2012-03-04 08:03:04','yyyy-mm-dd hh24:mi:ss'),to_date('2012-03-04 10:05:04','yyyy-mm-dd hh24:mi:ss'));
    insert into a values(a_squ.nextval,1,to_date('2012-03-04 12:03:04','yyyy-mm-dd hh24:mi:ss'),to_date('2012-03-04 15:03:07','yyyy-mm-dd hh24:mi:ss'));
    insert into a values(a_squ.nextval,1,to_date('2012-03-05 12:03:04','yyyy-mm-dd hh24:mi:ss'),to_date('2012-03-05 18:03:04','yyyy-mm-dd hh24:mi:ss'));
    insert into a values(a_squ.nextval,2,to_date('2012-03-04 12:03:04','yyyy-mm-dd hh24:mi:ss'),to_date('2012-03-04 13:03:04','yyyy-mm-dd hh24:mi:ss'));
    insert into a values(a_squ.nextval,2,to_date('2012-03-05 12:03:05','yyyy-mm-dd hh24:mi:ss'),to_date('2012-03-05 17:03:04','yyyy-mm-dd hh24:mi:ss'));---1、看结果有三个字段需要显示,车辆id直接呈现,日期,小时通过计算得到。--每天(每,各等关键字,使用group by),车辆工作小时=结束时间-开始时间
    SELECT a.cid 车辆id,sum((a.endtime-a.starttime)*24) 车辆工作小时,--*24是因为带时分秒的日期计算结果单位是天,要得到小时就乘以24
     trunc(a.starttime) 日期
    FROM a
    WHERE (这里填写日期统计的日期范围)
    GROUP BY trunc(a.starttime),a.cid --trunc函数得到无时无分无秒的日期