有一组机器每天都开机运行,每台机器每天一条记录,想查一下一段时间内(比如3天)都没开机的机器有哪些?
表名:comp 字段名:id(机器号)、rq(日期)、yxsj(每天运行时间)、yxnr(运行内容)....
我想查一下2010-04-01至2010-04-05这5天一直没开机的机器,我是这样做的:
select id,rq,yxsj from comp where rq>'2010-04-01' and rq<='2010-04-05' and (yxsj=0 or yxsj is null)
但结果不是我想要的,请问哪里出问题了?请大家指教,谢谢!

解决方案 »

  1.   

    select id
    from comp a,(select distinct id ,from comp where rq > '2010-01-01'
    ) b
    where a.id(+) = b.id
    a.rq>'2010-04-01' and a.rq<='2010-04-05' a.rq is null;
      

  2.   

     rq>'2010-04-01' and rq<='2010-04-05' 
    RQ的什么类型的?oracle 日期范围查询问题 
    select * from aaa where to_char(rq,'yyyymmdd') between '20011101' and '20020301';
    直接在rq上加函数,如果应用大(这个表内数据很多时),查询速度会相当慢的,为了提高查询速度,强烈建议这样书写:
    select * from aaa where rq between to_date('2001-11-01','yyyy-MM-DD') and to_date('2002-03-01' ,'YYYY-MM-DD');
    推荐使用
    select * from aaa where rq>;=to_date('2001-11-01','yyyy-MM-DD') and rq<=to_date('2002-03-01' ,'YYYY-MM-DD');用between的函数可能会慢些
      

  3.   

    日期格式不对rq>to_date() and rq<to_tate()或者用between and 
      

  4.   

    and (yxsj=0 or yxsj is null)
    你加这个条件表示你不开机也有记录?只是运行时间为0?
      

  5.   

    and (yxsj=0 or yxsj is null)
    对的,每天都有一条记录
      

  6.   

    我给个思路巴  你要查一直关机的话  就对关机的纪录做个COUNT 在 GROUP BY  极其ID  然后这个COUNT值等于 日期的差值的 ,就是一直关机的了
      

  7.   

    select id,count(yxsj) from aaa where rq>=to_date('2010-04-01','yyyy-MM-DD') and rq<=to_date('2010-04-05' ,'YYYY-MM-DD') and (yxsh=0 or yxsh is null) group by id having count(yxsj)=5;
      

  8.   

    上面yxsj写成yxsh了…… 改过来:select id,count(yxsj) from comp where rq>=to_date('2010-04-01','yyyy-MM-DD') and rq<=to_date('2010-04-05' ,'YYYY-MM-DD') and (yxsj=0 or yxsj is null) group by id having count(yxsj)=5;
      

  9.   

    回8楼、11楼:谢谢!不过11楼的语句最后应为 count(yxsj)=0
      

  10.   

    11楼是正确的
    应该是count(yxsj)=5
    因为是yxsj=0 or yxsj is null
      

  11.   

    如果时间段很长的话,大家看能不能把count(yxsj)=5 改为:sum(yxsj)=0呢(我周五的时候误把11楼的count当成sum了)?
      

  12.   

    刚刚试验了下:count(yxsj)=5和sum(yxsj)=0都得不到想要的结果
    用count(rq)=5可以得到结果,个人认为是不是having只统计有数据的列呢(rq列每天都有值,而yxsj列停机的时候,有时是0有时是NULL)?
    再次感谢大家!
      

  13.   

    select id,count(yxsj) from comp where rq>=to_date('2010-04-01','yyyy-MM-DD') and rq<=to_date('2010-04-05' ,'YYYY-MM-DD') group by id
    minus
    select id,count(yxsj) from comp where rq>=to_date('2010-04-01','yyyy-MM-DD') and rq<=to_date('2010-04-05' ,'YYYY-MM-DD') and (yxsj<>0 and yxsj is not null) group by id having count(yxsj)=5