简体   繁体   English

Oracle 设置运营商查询

[英]Oracle Set operator query

Have a SQL query on Oracle 11g which returns the count of whether a record having certain ID and status exists within +/- 15 minutes range in a table.对 Oracle 11g 进行 SQL 查询,该查询返回表中 +/- 15 分钟范围内是否存在具有特定 ID 和状态的记录的计数。

Now I wish to ignore the current date by adding a condition like AND TIMESTAMP < trunc(sysdate) .现在我希望通过添加AND TIMESTAMP < trunc(sysdate)之类的条件来忽略当前日期。
However, for cases where the record exists in todays date I wish to ignore the date comparison check in the query '2010-07-20 19:15:11' >= TO_CHAR(TIMESTAMP - (1/1440*15), 'YYYY-MM-DD HH24:MI:SS') AND '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') AND '2010-07-20 19:15:11' <= (TO_CHAR(TIMESTAMP + (1/1440*15), 'YYYY-MM-DD HH24:MI:SS'))

SELECT count(1) AS COUNT 
FROM MASTER_ONE 
WHERE ID='123' AND STATUS= 'ACTIVE' 
AND '2010-07-20 19:15:11' >= TO_CHAR(TIMESTAMP - (1/1440*15), 'YYYY-MM-DD HH24:MI:SS') 
AND '2010-07-20 19:15:11' <= (TO_CHAR(TIMESTAMP + (1/1440*15), 'YYYY-MM-DD HH24:MI:SS')) 
UNION ALL 
SELECT count(1) AS COUNT 
FROM MASTER_TWO 
WHERE ID='321' AND STATUS= 'ACTIVE' 
AND '2010-07-20 19:15:11' >= TO_CHAR(TIMESTAMP - (1/1440*15), 'YYYY-MM-DD HH24:MI:SS') 
AND '2010-07-20 19:15:11' <= (TO_CHAR(TIMESTAMP + (1/1440*15), 'YYYY-MM-DD HH24:MI:SS'))

How do I do this?我该怎么做呢?

The first problem with your query is that you're doing a string comparison on the date.您的查询的第一个问题是您正在对日期进行字符串比较。 Use to_date instead of to_char and let Oracle help you out.使用to_date代替to_char并让 Oracle 帮助您。

SELECT   
    to_date('2010-07-20 19:15:11', 'YYYY-MM-DD HH24:MI:SS') AS orig_date
  , to_date('2010-07-20 19:15:11', 'YYYY-MM-DD HH24:MI:SS') - 1 / 24 / 4 AS fifteen_min_prior
  , to_date('2010-07-20 19:15:11', 'YYYY-MM-DD HH24:MI:SS') + 1 / 24 / 4 AS fifteen_min_after
FROM dual;

Output: 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 those dates in a BETWEEN condition in the predicate.然后使用可以在谓词的BETWEEN条件中使用这些日期。 See Oracle date "Between" Query .请参阅Oracle 日期“之间”查询

I'm not quite clear what you mean by "However, for cases where the record exists in todays date I wish to ignore the date comparison check in the query."我不太清楚“但是,对于今天存在记录的情况,我希望忽略查询中的日期比较检查”是什么意思。 You'd just written that you want to exclude values from the current day.您刚刚写过要排除当天的值。 Either you're excluding today's records or you're not.您要么排除今天的记录,要么不排除。

Ok, you can try something like this, if I understood you correctly:好的,如果我理解正确,您可以尝试这样的事情:

SELECT count(1) AS COUNT 
FROM MASTER_ONE 
WHERE ID='123' AND STATUS= 'ACTIVE' 
AND (timestamp > trunc(sysdate) 
  OR (timestamp < trunc(sysdate) 
  AND timestamp BETWEEN to_date(:yourInputDate,'DD/MM/YYYY HH24:MI:SS') - (1/1440*15) 
                    AND to_date(:yourInputDate,'DD/MM/YYYY HH24:MI:SS') + (1/1440*15)))
UNION ALL
SELECT count(1) AS COUNT 
FROM MASTER_TWO 
WHERE ID='321' AND STATUS= 'ACTIVE' 
AND (timestamp > trunc(sysdate) 
  OR (timestamp < trunc(sysdate) 
  AND timestamp BETWEEN to_date(:yourInputDate,'DD/MM/YYYY HH24:MI:SS') - (1/1440*15) 
                    AND to_date(:yourInputDate,'DD/MM/YYYY HH24:MI:SS') + (1/1440*15)))

In this Select, you only apply the 15 minutes condition if your timestamp column has a date prior to sysdate.在此 Select 中,如果您的时间戳列的日期早于 sysdate,则仅应用 15 分钟条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM