Only want report to load last 30 min of data - sql

I am trying to have an APEX report show only responses submitted in the last 30 minutes. Is it possible to do this with the where clause?
I originally tried using a trigger that feed into a creation date column but I had issues with data showing up into the column and was unable to have the report only show the past 30 minutes.
Any ideas would be helpful,
Thanks!
select SCRAP_DATE,
PAINT_SHOP,
IGEF,
GIRDER,
MODEL,
COLOR,
SCRAP_OWNER,
sysdate
from SCRAP_BODY_SYSTEM
where sysdate >= (current_timestamp - interval '30' minute)

where clause is the way to do it, e.g.
where date_column >= sysdate - interval '30' minute
because of this:
SQL> select sysdate,
2 sysdate - interval '30' minute half_an_hour_ago
3 from dual;
SYSDATE HALF_AN_HOUR_AGO
---------------- ----------------
04.05.2022 14:37 04.05.2022 14:07
SQL>
sysdate vs. current_timestamp:
SQL> select sysdate, current_timestamp from dual;
SYSDATE CURRENT_TIMESTAMP
------------------- -------------------------------
05.05.2022 07:48:19 05.05.22 07:48:19,200858 +02:00
SQL>

I believe you could use a where condition which would be something like:
where submitted_datetime >= (current_timestamp - interval '30' minute)
Do let me know if it works!

Related

Apex Report to load last 30 min of data

I am trying to have an APEX report show only responses submitted in the last 30 minutes. Is it possible to do this with the where clause? Getting stuck with all of this errors shown below:
Here is my current code:
select SCRAP_DATE,
PAINT_SHOP,
MODEL,
COLOR,
SCRAP_OWNER,
DEPARTMENT_CODE,
REASON_FOR_SCRAP,
SCRAP_TYPE,
BODY_TYPE,
DETAILS,
COMMENTS,
SUBMITTED_DATETIME
from SCRAP_BODY_SYSTEM
SQL> select sysdate,
2 sysdate - interval '30' minute half_an_hour_ago
3 from dual;
SYSDATE HALF_AN_HOUR_AGO
---------------- ----------------
04.05.2022 14:37 04.05.2022 14:07
SQL>
SQL> select sysdate, current_timestamp from dual;
SYSDATE CURRENT_TIMESTAMP
------------------- -------------------------------
05.05.2022 07:48:19 05.05.22 07:48:19,200858 +02:00
SQL>
where submitted_datetime >= sysdate - interval '30' minute
You really shouldn't copy/paste my SQL*Plus session (from your previous question) into Apex (including the SQL> prompt) and expect it to work.
Code that might work is
select SCRAP_DATE,
PAINT_SHOP,
MODEL,
COLOR,
SCRAP_OWNER,
DEPARTMENT_CODE,
REASON_FOR_SCRAP,
SCRAP_TYPE,
BODY_TYPE,
DETAILS,
COMMENTS,
SUBMITTED_DATETIME
from SCRAP_BODY_SYSTEM
where submitted_datetime >= sysdate - interval '30' minute;

How to truncate timestamp to minutes?

I have to subtract 5 minutes from current timestamp and floor(truncate) it to nearest minute. Like '2016-02-23 06:10:39.0' should be '2016-02-23 06:05:00.0'.
I have found way to subtract 5 minutes as
systimestamp - interval '5' minute
EDIT 1:
I need timestamp in particular format,
TO_TIMESTAMP((systimestamp - interval '15' minute),'YYYY-MM-DD HH24:MI:SS.ff')
But this is giving
ORA-01830: date format picture ends before converting entire input string
But I am not able to floor it to nearest minute.
Please help. Thanks
You could use TRUNC() with the precision you want. To trunc only till minutes, use MI.
For example,
SQL> SELECT SYSTIMESTAMP, trunc(SYSTIMESTAMP - INTERVAL '5' MINUTE, 'MI') new_tmstmp
2 FROM dual;
SYSTIMESTAMP NEW_TMSTMP
----------------------------------- -------------------
16-MAR-16 04.44.02.379000 PM +05:30 03/16/2016 16:39:00
SQL>
Remember, the above output will be a DATE and not TIMESTAMP. You can explicitly CAST the date as timestamp:
SQL> SELECT SYSTIMESTAMP,CAST(trunc(SYSTIMESTAMP - INTERVAL '5' MINUTE,'MI') AS TIMESTAMP) tm
2 FROM dual;
SYSTIMESTAMP TM
----------------------------------- ------------------------------
16-MAR-16 04.53.25.802000 PM +05:30 2016-03-16 04:48:00.000000
SQL>
You can use trunc():
trunc(systimestamp - interval '5' minute, 'minute')

Using "Interval" in Oracle where "Interval" is a value from a table

I need to generate a list of values in an Oracle DB with the following columns of data:
ITEM_TYPE VARCHAR2(20)
ITEM_LAST_UPDATED DATE
ITEM_UPDATE_TOLERANCE NUMBER(1)
The only data that should be send out to the console would be items that have the date in 'ITEM_LAST_UPDATED' less than the sysdate minus the integer value within 'ITEM_UPDATE_TOLERANCE'.
So, if I wanted to just show the ones that were one hour past due, I can do:
select ITEM_TYPE from MY_TABLE
where
to_char(ITEM_LAST_UPDATED, 'DD-MON-YYYY HH24:MI')
<=
to_char(sysdate - interval '1' hour, 'DD-MON-YYYY HH24:MI');
However, rather than using the '1' in the above statement, I need to replace it with the numeric value of ITEM_UPDATE_TOLERANCE.
I tried several different versions, but all error (such as):
select ITEM_TYPE from MY_TABLE
where
to_char(ITEM_LAST_UPDATED, 'DD-MON-YYYY HH24:MI')
<=
to_char(sysdate - interval to_number(ITEM_UPDATE_TOLERANCE) hour, 'DD-MON-YYYY HH24:MI');
Why are you converting a perfect DATE column to a character value just to compare it another DATE value converted to a character column.
Simply use:
ITEM_LAST_UPDATED <= sysdate - interval '1' hour
To achieve what you want, just multiply the value:
ITEM_LAST_UPDATED <= sysdate - (interval '1' hour) * ITEM_UPDATE_TOLERANCE
There is also absolutely no need to convert a number to a number using the to_number() function.
As an alternative to #a_horse_with_no_name's interval multiplication trick, or San's division method, you can also use the numtodsinterval() function:
ITEM_LAST_UPDATED <= sysdate - numtodsinterval(ITEM_UPDATE_TOLERANCE, 'HOUR')
As an example:
select sysdate, sysdate - numtodsinterval(3, 'HOUR') from dual;
SYSDATE SYSDATE-NUMTODSINTE
------------------- -------------------
2014-03-07 19:08:27 2014-03-07 16:08:27
Well you can try using simple calculation
select ITEM_TYPE from MY_TABLE
where
ITEM_LAST_UPDATED
<=
sysdate - (ITEM_UPDATE_TOLERANCE/24);
Calculation of ITEM_UPDATE_TOLERANCE/24 will convert hours into days and then can be subtracted from sysdate.

SYSDATE but specify the time

I want to say the follow but substitute the date with SYSDATE but the time between is what I want to adjust. What would the syntax be?
where mydatefield between SYSDATE+'0001' and SYSDATE+'2359'
...
WHERE TO_CHAR( MOPACTIVITY.MOPNOTIFICATIONSENDAT , 'yyyy-mm-dd hh24:mi' )
BETWEEN '2013-07-26 00:00:01' AND '2013-07-26 23:59:59'
;
SYSDATE (or any other date column) in Oracle has the time component. So you need to strip that off and then add the hours/minutes/time condition.
Eg. to say current day 10:00 AM to 3:00 PM, you can say
date_column between (trunc(sysdate) + 10/24) and (trunc(sysdate) + 15/24)
Oracle date arithmetic works on the day level. so, +1 will give you the next day, 1/24 will give you an hour and 10/24 will give you 10:00 AM in the current day.
SQL> alter session set nls_date_format = 'DD-Mon-YYYY HH:MI:SS AM';
Session altered.
1 select sysdate,
2 trunc(sysdate),
3 trunc(sysdate) + 10/24,
4 trunc(sysdate) + 15/24
5* from dual
SQL> /
SYSDATE 26-Jul-2013 06:26:07 PM
TRUNC(SYSDATE) 26-Jul-2013 12:00:00 AM
TRUNC(SYSDATE)+10/24 26-Jul-2013 10:00:00 AM
TRUNC(SYSDATE)+15/24 26-Jul-2013 03:00:00 PM
For your question, you seem to be interested between current day and next day, so you can try adding + 1 to the date directly, once you strip the time component.
date_column >= trunc(sysdate) and
date_column < trunc(sysdate)+1
The best way to do this is to leave your MOPACTIVITY.MOPNOTIFICATIONSENDAT as a DATE type. That allows Oracle to optimize the query if there happens to be an index on the column. I'd recommend something like this:
WHERE MOPACTIVITY.MOPNOTIFICATIONSENDAT >= TRUNC(SYSDATE)
AND MOPACTIVITY.MOPNOTIFICATIONSENDAT < TRUNC(SYSDATE) + 1
That boils down to "greater than or equal to today at midnight" and "less than tomorrow at midnight".
We can also trunc both the dates and then compare the result
where TRUNC(MOPACTIVITY.MOPNOTIFICATIONSENDAT) = TRUNC(SYSDATE)
TRUNC Removes the timestamp from the dates

Sysdate vs Timestamp for comparisons

I'm checking for product updates that are dependent on time of day - alerting if a product has not updated in the last 2 hours , e.g.:
03:30 - 07:29 we expect only product x to be updating;
07:30 - 11:29 we expect both product x and y to be updating
11:30 - 15:30 we expect only product y to be updating
But we need to be sensitive to products that have not updated yesterday and still have not updated today. Thus my question - when doing the below timestamp comparisons for product y, I suspect that date is not factored in and therefore a product that hadn't updated potentially gets ignored.
(...)
AND inv.timestamp < sysdate - (120 / 1440) --older than two hours
AND inv.timestamp > TRUNC(sysdate) + (690/1440) --after 11:30
AND inv.timestamp < TRUNC(sysdate) + (930/1440) --before 15:30
(...)
inv.timestamp is a column in timestamp(6) type. Does trunc(sysdate) implicitly include the present date for comparison or is it only using the time?
TRUNC(SYSDATE) returns 00:00:00 of the current day.
SQL> select trunc(sysdate) from dual;
TRUNC(SYSDATE)
-------------------
2012-03-09 00:00:00
Documentation on TRUNC
As eaolson and Ben have already noted, SYSDATE includes the date AND time. Using TRUNC "removes" the time element, i.e. resets it to midnight of that day.
You may want to consider using the INTERVAL functionality provided by Oracle, to make it clearer what the SQL is doing, e.g.
SELECT to_char(trunc(sysdate) + interval '11' hour + interval '30' minute,'dd/mm/yyyy hh24:mi:ss') sysdate_trunced_to_1130,
to_char(trunc(current_timestamp) + interval '11' hour + interval '30' minute,'dd/mm/yyyy hh24:mi:ss') timestamp_trunced_to_1130
FROM dual;
Glancing at the code, "(690/1440)" doesn't immediately suggest 11:30 in the morning, but + interval '11' hour + interval '30' minute hopefully is a little better.