Query to produce data only within the asked hour range - sql

Oracle SQL:
I been trying to get this some snippet of query working. When you run it, it prompts you for an hour, and displays no records. I don’t want the query to have a static hour as it will need to be run 4 times a day.
So from the ‘&date’ input, I want it to show data for the past 24 hours. Is that possible?
dt_time = timestamp(6) field
select distinct to_char(dt_ time,'dd/mm/yyyy hh24'), fault_description
from order
where to_char(dt_time,'hh24') <= '&date' -24
order by to_char(dt_ time,'dd/mm/yyyy hh24');
Example, if you enter 10 (when query executed) it will show the data from 10(:00) through to 10(:00)next day
[Hope there is enough info for someone to answer, please]

Use a combination of between and interval:
where dt_time between
to_date(&date, 'ddmmyyyy hh24') and
to_date(&date, 'ddmmyyyy hh24') + interval '24' hour
You'll need to pass/parse the input date as a full date and not only the hour part, in order to prevent unexpected results.

You can use NUMTODSINTERVAL in a CTE to get the hour as user input.
WITH t_hour( h ) AS
( SELECT NUMTODSINTERVAL(&d,'HOUR' ) h FROM DUAL
)
SELECT DISTINCT TO_CHAR(dt_time,'dd/mm/yyyy hh24'),
fault_description
FROM ORDER
CROSS JOIN t_hour
WHERE dt_time BETWEEN TRUNC(SYSDATE) + h AND TRUNC(SYSDATE) + 1 + h
ORDER BY 1;

You need to make use of SYSDATE as you are fetching last one day data.
Also if you add or subtract number with a date column the offset will be number of days, not number of hours. So you need to use &date/24
Try this:
select distinct to_char(dt_time,'dd/mm/yyyy hh24'), fault_description
from order
where dt_time - &date / 24 between TRUNC(sysdate-1) and TRUNC(sysdate)
order by to_char(dt_ time,'dd/mm/yyyy hh24');
Please comment.

WHERE DATEDIFF(SYSDATE,DT_TIME) * 24 < &DATE AND DT_TIME < SYSDATE

Related

How to search by to_date function?

I have table as below:
Table Temp:
ID MAX MIN DATE_C
1 34 24 21-APR-17 02.41.38.520000 PM
2 32 26 20-APR-17 02.42.44.569000 PM
I execute the below SQL query to get temperature details on respective date:
SELECT *
FROM Temp t
WHERE t.date_c = TO_DATE( '2017-04-21', 'YYYY-MM-DD')
order by t.id
But it's returning empty records. Whats wrong with my query?
You need to remove the time component on the column. Here is one way:
SELECT *
FROM Temp t
WHERE TRUNC(t.date_c) = DATE '2017-04-21'
ORDER BY t.id;
However, I usually recommend using inequalities, rather than a function on the column:
SELECT *
FROM Temp t
WHERE t.date_c >= DATE '2017-04-21' AND
t.date_c < DATE '2017-04-22'
ORDER BY t.id;
This allows the query to use an index on date_c. I should add that the original version can use an index on (trunc(date_c, id).
21-APR-17 02.41.38.520000 PM is not a DATE; it has a fractional seconds component so it is a TIMESTAMP.
So, if you want to find items that are on a particular day (inputting the TIMESTAMP using an ISO/ANSI timestamp literal):
SELECT *
FROM Temp
WHERE date_c >= TIMESTAMP '2017-04-21 00:00:00' AND
date_c < TIMESTAMP '2017-04-21 00:00:00' + INTERVAL '1' DAY;
or
SELECT *
FROM Temp
WHERE date_c >= TO_TIMESTAMP( :your_date_string, 'YYYY-MM-DD' ) AND
date_c < TO_TIMESTAMP( :your_date_string, 'YYYY-MM-DD' ) + INTERVAL '1' DAY;
it's returning empty records. Whats wrong with my query?
date_c = TO_DATE( '2017-04-21', 'YYYY-MM-DD') matches all rows where the date_c value is exactly 2017-04-21 00:00:00.000000 (including the time component); if you do not have any rows with exactly that date and time then, as you noticed, it will return nothing. If you want to get records matching that day then you need to get values within a range of times between the start and end of the day.
You need to pass date on the column. Here is a way...
SELECT *
FROM Temp t
WHERE CAST(t.CREATED_ON as date)= N'2017-04-22'
ORDER BY t.id

Add days Oracle SQL

SELECT ORDER_NUM, CUSTOMER_NUM, CUSTOMER_NAME, ADD_DAYS (ORDER_DATE, 20)
FROM CUSTOMER, ORDERS;
Oracle Express says ADD_DAYS invalid? Any ideas what Am I doing wrong?
If you want to add N days to your days. You can use the plus operator as follows -
SELECT ( SYSDATE + N ) FROM DUAL;
You can use the plus operator to add days to a date.
order_date + 20
In a more general way you can use "INTERVAL". Here some examples:
1) add a day
select sysdate + INTERVAL '1' DAY from dual;
2) add 20 days
select sysdate + INTERVAL '20' DAY from dual;
2) add some minutes
select sysdate + INTERVAL '15' MINUTE from dual;
Some disadvantage of "INTERVAL '1' DAY" is that bind variables cannot be used for the number of days added. Instead, numtodsinterval can be used, like in this small example:
select trunc(sysdate) + numtodsinterval(:x, 'day') tag
from dual
See also: NUMTODSINTERVAL in Oracle Database Online Documentation
It's Simple.You can use
select (sysdate+2) as new_date from dual;
This will add two days from current date.
One thing about
select (sysdate+3) from dual
is that the sysdate is interpreted as date.
But if you want to use a custom date, not local, you need to make sure it is interpreted as date, not string. Like so (adding 3 days):
select (to_date('01/01/2020')+3) from dual

Most recent child record keyed

How do I get the date of the most recent child record? In the SQL, I am trying to get the latest MOPNOTES.MOPNOTEDATE. I am also wanting only one record per MOPACTIVITY.MOPID. Any direction would be greatly appreciated.
SELECT DISTINCT MOPACTIVITY.MOPID,
MOPNOTES.MOPNOTEDATE
FROM MOPUSER.MOPACTIVITY INNER JOIN MOPUSER.MOPNOTES ON MOPACTIVITY.MOPID=MOPNOTES.MOPID
WHERE MOPACTIVITY.MOPEND BETWEEN TRUNC(SYSDATE-1) + INTERVAL '12:00' HOUR TO MINUTE AND TRUNC(SYSDATE) + INTERVAL '9:00' HOUR TO MINUTE
AND UPPER(MOPACTIVITY.MOPSTATUS) = 'COMPLETE'
AND UPPER(MOPACTIVITY.MOPNOTIFICATIONSENT) LIKE '%TRUE%'
ORDER BY MOPACTIVITY.MOPID
You're looking for the MAX aggregate function on a date field in combination with a group by on the MOPID to Group all like Ids and return the most recent date. Distinct wasn't working for you becuase you had different dates. By only returning the max you get 1 date and 1 ID is generated using the group by.
SELECT MOPACTIVITY.MOPID, Max(MOPNOTES.MOPNOTEDATE)
FROM MOPUSER.MOPACTIVITY
INNER JOIN MOPUSER.MOPNOTES
ON MOPACTIVITY.MOPID=MOPNOTES.MOPID
WHERE MOPACTIVITY.MOPEND BETWEEN TRUNC(SYSDATE-1) + INTERVAL '12:00' HOUR TO MINUTE AND TRUNC(SYSDATE) + INTERVAL '9:00' HOUR TO MINUTE
AND UPPER(MOPACTIVITY.MOPSTATUS) = 'COMPLETE'
AND UPPER(MOPACTIVITY.MOPNOTIFICATIONSENT) LIKE '%TRUE%'
GROUP BY MOPACTIVITY.MOPID
ORDER BY MOPACTIVITY.MOPID

SQL date check within set number of days

Im trying to find out how many clients viewed a property within 14 days of May 20, 2004, either before or after. Not really sure at all how to go about this.
Im assuming i need to group it and use a having?
EDIT: I am using oracle now
select count(*)
from VIEWING
WHERE CLAUSE?
For a one time query with that specific date,
select count(*) clients
from yourtable
where yourdatefield >= {d'2004-05-06'}
and yourdatefield < {d'2004-06-08'}
You might want to consult a calendar to see if those dates are correct.
Edit #1, since you are using Oracle, you can use:
select count(*) TotalClients
from yourtable
where dt >= (to_date('2004-05-20', 'yyyy-mm-dd') - INTERVAL '14' DAY)
and dt <= (to_date('2004-05-20', 'yyyy-mm-dd') + INTERVAL '14' DAY)
See SQL Fiddle with Demo
Based on some of your previous questions you were using MySQL.
If you are using MySQL then you can use the DATE_ADD() function to get the date range and then use count(*) to return all records from those dates:
select count(*) TotalClients
from yourtable
where dt >= date_add(str_to_date('2004-05-20', '%Y-%m-%d'), INTERVAL -14 day)
and dt <= date_add(str_to_date('2004-05-20', '%Y-%m-%d'), INTERVAL 14 day)

How can I group by date time column for custom hour

How can I group by date time column for custom hour, for example every 5 Hour or every 12 Hour or every 24 hour.
Select CreateOn, Count(*) From Table1
Group By ?????
You should select start date/time, '2011-01-01' in this example and then use DATEDIFF() function to get time difference in hours between start date and CreateOn. Then use / operator to get an integer interval number (5 hours in this example).
Select
min(DATEADD(HOUR,DATEDIFF (HOUR,'2011-01-01',CreateOn )/5*5,'2011-01-01'))
as Start_time,
max(DATEADD(HOUR,(DATEDIFF (HOUR,'2011-01-01',CreateOn )/5+1)*5,'2011-01-01'))
as End_Time,
DATEDIFF (HOUR,'2011-01-01',CreateOn )/5 as Interval_Number,
Count(*) as _Count
From Table1
Group By DATEDIFF (HOUR,'2011-01-01',CreateOn )/5
If I understand your question correctly, you could try something like this:
SELECT CreateOn, Count(*) FROM Table1 GROUP BY (DATEPART(MINUTE, [Date]) % 12)