sql - Oracle Set operator query -
have sql query on oracle 11g returns count of whether record having id , status exists within +/- 15 minutes range in table.
now wish ignore current date adding condition and timestamp < trunc(sysdate)
.
however, cases record exists in todays date wish ignore date comparison check in query '2010-07-20 19:15:11' >= to_char(timestamp - (1/1440*15), 'yyyy-mm-dd hh24:mi:ss') , '2010-07-20 19:15:11' <= (to_char(timestamp + (1/1440*15), 'yyyy-mm-dd hh24:mi:ss'))
select count(1) count master_one id='123' , status= 'active' , '2010-07-20 19:15:11' >= to_char(timestamp - (1/1440*15), 'yyyy-mm-dd hh24:mi:ss') , '2010-07-20 19:15:11' <= (to_char(timestamp + (1/1440*15), 'yyyy-mm-dd hh24:mi:ss')) union select count(1) count master_two id='321' , status= 'active' , '2010-07-20 19:15:11' >= to_char(timestamp - (1/1440*15), 'yyyy-mm-dd hh24:mi:ss') , '2010-07-20 19:15:11' <= (to_char(timestamp + (1/1440*15), 'yyyy-mm-dd hh24:mi:ss'))
how do this?
the first problem query you're doing string comparison on date. use to_date
instead of to_char
, let oracle out.
select to_date('2010-07-20 19:15:11', 'yyyy-mm-dd hh24:mi:ss') orig_date , to_date('2010-07-20 19:15:11', 'yyyy-mm-dd hh24:mi:ss') - 1 / 24 / 4 fifteen_min_prior , to_date('2010-07-20 19:15:11', 'yyyy-mm-dd hh24:mi:ss') + 1 / 24 / 4 fifteen_min_after dual;
output:
orig_date fifteen_min_prior fifteen_min_after ------------------------- ------------------------- ------------------------- 20-jul-10 07:15:11 pm 20-jul-10 07:00:11 pm 20-jul-10 07:30:11 pm
then use can use dates in between
condition in predicate. see oracle date "between" query.
i'm not quite clear mean "however, cases record exists in todays date wish ignore date comparison check in query." you'd written want exclude values current day. either you're excluding today's records or you're not.
Comments
Post a Comment