有一组机器每天都开机运行,每台机器每天一条记录,想查一下一段时间内(比如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)
但结果不是我想要的,请问哪里出问题了?请大家指教,谢谢!
表名: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)
但结果不是我想要的,请问哪里出问题了?请大家指教,谢谢!
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;
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的函数可能会慢些
你加这个条件表示你不开机也有记录?只是运行时间为0?
对的,每天都有一条记录
应该是count(yxsj)=5
因为是yxsj=0 or yxsj is null
用count(rq)=5可以得到结果,个人认为是不是having只统计有数据的列呢(rq列每天都有值,而yxsj列停机的时候,有时是0有时是NULL)?
再次感谢大家!
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