SQL Query to fetch data between minutes and days - sql

I have requirement, this is for a report.
Requirement description:
Need data from a table which are in OPEN status for more than 30 minutes
Well, the above requirement can be obtained from
select *
from xyz
where status = 'OPEN'
and last_update_date <= sysdate - (30/1440) --30 is minutes and its a parameter
the above query will fetch all the data which are in OPEN status from beginning to sysdate - (30/1440). so i want to modify the query to restrict the complete data , by adding another parameter like DAY
for example if i give 10 as day, it should fetch all the data only in the last 10 days and sysdate-30 minutes.
we should use last_update_date column for restricting the day.
If I dont give any day as input if should fetch all the records from sysdate-30 minutes.
If I dont give minutes it should fetch all the records in OPEN status.
is the question clear enough? My English is bad.
Please suggest me a query..

Simply use OR expressions where you check the parameters for NULL:
select *
from xyz
where status = 'OPEN'
and (last_update_date <= sysdate - (:minutes/1440) or :minutes is null)
and (trunc(last_update_date) >= trunc(sysdate - :days) or :days is null)

This query is not tested.
Please tell me this query is returning any error or somthing that i have missed.
select
*
from
xyz
where
status = 'OPEN'
and
last_update_date <= CASE WHEN #No_Of_Days <> 0 THEN sysdate - #No_Of_Days ELSE NULL END
and
minutes(last_update_date) <= SYSDATE - 30/1440

Related

Stored procedure - to print my result from SQL query

select count(*) as count
from [dbo].[DATA_received] where DATEDIFF(MINUTE, DATE_TIME GETDATE()) <= 1
I want to run this in some frequency to check whether I am receiving data in DB.
I want to write stored procedure where if my results 0 then ,"No data received"
If the count is >0 then it should print "Data received"
Not sure how you're going to make SolarWinds do something with this output or run it every minute, but there is absolutely no reason to count how many rows were added in the last minute if you only need to know whether at least one such row exists... and you really should never apply formulae like DATEDIFF to a column, it will force the query to scan the entire table every time:
SELECT MessageToPrint = CASE WHEN EXISTS
(
SELECT 1 FROM dbo.DATA_received
WHERE DATE_TIME >= DATEADD(MINUTE, -1, sysutcdatetime())
) THEN 'Data received.' ELSE 'No data received.' END;

Select excluding certain time of the day

So I'm trying to query records that don't have a specific timestamp, I don't want to see any records that have a time of 01:00:00 AM
select * from records
where to_char(record_time,'hh24') not between 1 and 1
But I'm still not getting the result set I'm looking for, any help is appreciated.
Try:
SELECT *
FROM RECORDS
WHERE NVL(TO_CHAR(record_time,'HH24:MI:SS AM'),'X') <> '01:00:00 AM';
You can compare the time with the time truncated back to midnight with one hour added and since NULL is never equal to anything you can test for that separately:
SELECT *
FROM records
WHERE record_time != TRUNC(record_time) + INTERVAL '1' HOUR
OR record_time IS NULL;

Declaration of the date parameter with the automatic addition of the month in the sql query

I work in SQL Developer by Oracle. I have a longer query where I have multiple dates in some conditions but every dates based on a start_date and only difference between them are months and days.
I want to declare only one date e.g. start_date='2021-06-01' and afterwards in query where I have condition like COLUMN_DATE BETWEEN DATE '2021-08-01' AND DATE '2021-08-31' only add months (in that example add 2 months in query and get the results from whole August/ e.g. 2021-08-01=start_date+(2months)). Is it possible to get results like that without entering each value separately? Below is my sample code.
Def start_date='2021-06-01'
Select
1column,
2column,
(case when exist(select 1
from table2
where between date '2021-08-01' and date '2021-08-31')
then 1 else 0 end) as 3column
from table1;
Use ADD_MONTHS and pass in your substitution variable:
Select column1,
column2,
case
when exist(select 1
from table2 t2
where t2.date_column >= ADD_MONTHS(TO_DATE(&start_date, 'YYYY-MM-DD'), 2)
and t2.date_column < ADD_MONTHS(TO_DATE(&start_date, 'YYYY-MM-DD'), 3)
)
then 1
else 0
end as column3
from table1;
Note: In Oracle, a DATE always has a time component (the user interface you are using may chose not to show the time component though, but it will still be there) so if you want a month's worth of data and you compare to DATE '2021-08-31' then you will miss any values between 2021-08-31 00:00:01 and 2021-08-31 23:59:59.

Query records which have been in a certain status for one hour or more

So I have a question (running oracle sql developer), I can't seem to get the syntax of it right.
Let's say T1 has a stat_code column and a last_updated column; I want to query all records which have been in t1.stat_code < 90 for one hour or more.
From research I have come with the following query;
select * from t1
where t1.stat_code <90
and t1.last_updated > (SYSDATE-1/24);
This is right, but also pulls back records which are less than an hour old.
Any help is very much appreciated!
You could also use an INTERVAL literal to explicitly state the duration:
SELECT *
FROM t1
WHERE stat_code < 90
AND last_updated <= SYSTIMESTAMP - INTERVAL '1' HOUR;

Increments of a date using Oracle SQL

We send mailouts every evening using SQL. For this in particular we'd like to send a reminder every 3 days, but I have absolutely no idea how to do this, or if it's even possible?
Here's my code:
SELECT *
FROM table
WHERE status = 'pending'
AND ( date_created = sysdate - 3
OR date_created = sysdate - 6
OR date_created = sysdate - 9 )
Something to that effect, but a nicer way of coding it and also a more effective one that won't require me to go all the way into the hundreds! ^_^
Thanks
Try
SELECT *
FROM table
WHERE status = 'pending'
AND MOD(sysdate-date_created, 3) = 0
Information on Oracle MOD: http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions088.htm