How to get data on minute base instead of hourly base? - sql

I use the following query to get my data counted on hourly base:
select to_char(trunc(time, 'HH'), 'DD/MM/YY HH24') as "date",
count(event), count(distinct id) from source
where time >= date '2021-01-01'
group by trunc(time,'HH')
order by trunc(time,'HH');
What I now want to do is the same but on minute base. How can I get this done?

To me, you could have avoided trunc entirely as you use TO_CHAR anyway:
select to_char(time, 'dd/mm/yy hh24') ...
If you want to include minutes, no problem:
select to_char(time, 'dd/mm/yy hh24:mi') as "date",
count(event), count(distinct id)
from source
where ...
group by to_char(time, 'dd/mm/yy hh24:mi')
Note that sorting by strings probably won't produce desired results. Perhaps, if you'd change date format to e.g. 'yyyymmdd hh24:mi'

Related

Oracle substract previous row

I've got this query:
SELECT user_id, from_loc_id, to_loc_id, to_char(dstamp, 'hh24:mi:ss')
FROM inventory_transaction
WHERE code = 'Pick'
AND substr(work_group,1,6) = 'BRANCH'
AND dstamp BETWEEN to_date('24/02/2022 17:00:00', 'dd/mm/yyyy hh24:mi:ss') AND
to_date('24/02/2022 18:00:00', 'dd/mm/yyyy hh24:mi:ss')
ORDER BY user_id;
That's the output:
My expected output is:
I was trying to use lag, but didn't really worked.
I've just realized I need to add a second ORDER BY, so first by user, second by to_char(dstamp, 'hh24:mi:ss').
All solutions much appreciate. Thank you.
You can use NUMTODSINTERVAL function with day argument and applying SUBSTR to extract hours:minutes:seconds portion as your data resides within a specific date such as
SELECT t.user_id,
t.dstamp,
SUBSTR(
NUMTODSINTERVAL(dstamp - LAG(dstamp)
OVER (PARTITION BY user_id ORDER BY dstamp),'day'),
12,8) AS time_diff
FROM t
Demo
Edit : The case above is applied for the column dstamp is considered to be of date data type, if its data type is timestamp, then use the following query containing date cast instead
SELECT t.user_id,
t.dstamp,
SUBSTR(
NUMTODSINTERVAL(CAST(dstamp AS date) - LAG(CAST(dstamp AS date))
OVER (PARTITION BY user_id ORDER BY CAST(dstamp AS date)),'day'),
12,8) AS time_diff
FROM t
Demo

Fetch data from Oracle Database on hourly and daily basis using timestamp and current date

I have written query to get data for a particular day, but need a query to get the data on hourly and daily basis
My Query:
SELECT
TRAN_USERTRANSACTIONS.APPLICATIONNUMBER
FROM
TRAN_USERTRANSACTION
WHERE TRAN_USERTRANSACTIONS.CREATEDAT
LIKE '%19-09-19%'
GROUP BY
TRAN_USERGTRANSACTION.APPLICATIONNUMBER;
CREATEDAT is Timestampin in format as follows :DD-MM-YY hh.mm.ss.ssss AM/PM
By hour(hope this helps):
Select TRAN_USERTRANSACTIONS.APPLICATIONNUMBER
, TO_CHAR(TRAN_USERTRANSACTIONS.CREATEDAT ,'HH AM')
from TRAN_USERTRANSACTION
WHERE TRAN_USERTRANSACTIONS.CREATEDAT LIKE '%19-09-19%'
group by TRAN_USERTRANSACTIONS.APPLICATIONNUMBER
, TO_CHAR(TRAN_USERTRANSACTIONS.CREATEDAT ,'HH AM')
order by TO_CHAR(dat_azu,'HH AM') asc;
I assume you want a summary by hour and not by application. If so, the hour should be in the GROUP BY:
SELECT TO_CHAR(t.CREATEDAT, 'YYYY-MM-DD HH24') as dh,
COUNT(*)
FROM TRAN_USERTRANSACTION t
WHERE t.CREATEDAT >= DATE '2019-09-19' AND
t.CREATEDAT < DATE '2019-09-20'
GROUP BY TO_CHAR(t.CREATEDAT, 'YYYY-MM-DD HH24')
ORDER BY dh;

Query to show per week, per day, per hour

I'm working in PLSQL, trying to find event counts for a given week, broken down by day and hour.
Essentially I want to plug in the week and then have the count of events such that I can easily discern counts >1M per day AND 300k per hour (=GOLD), 500k per day and 200k per hour (silver), 100k per day 30k per hour.
I'm trying something like this to get data per day, but I'm not sure how to break it down per day, per hour. Oracle rookie here.
SELECT TO_CHAR(TRUNC(store.transaction_datetime, 'HH'), 'DD-MON-YYYY HH24:MI:SS'), Identifier,
COUNT (*)
FROM data.stats store
WHERE store.transaction_datetime >= '2016-09-04 00:00:00'
AND store.transaction_datetime <= '2016-09-10 23:59:59'
GROUP BY Identifier, TO_CHAR (TRUNC (store.transaction_datetime, 'HH'), 'DD-MON-YYYY HH24:MI:SS')
ORDER BY TO_CHAR (TRUNC (store.transaction_datetime, 'HH'), 'DD-MON-YYYY HH24:MI:SS') ASC;
anything I can throw into excel and perform counts will work for me
any help appreciated
Let me know if it helps:
SELECT T.*,
CASE WHEN CNT_PER_DAY > ... AND CNT_PER_HOUR > ... THEN 'GOLD'
WHEN CNT_PER_DAY > ... AND CNT_PER_HOUR > ... THEN 'SILVER'
WHEN CNT_PER_DAY > ... AND CNT_PER_HOUR > ... 'BRONZE'
ELSE ...
END CATEGORY
FROM (
SELECT DISTINCT
Identifier,
TO_CHAR(TRUNC(store.transaction_datetime, 'HH'), 'DD-MON-YYYY HH24:MI:SS') HOUR_TIME,
COUNT (*) OVER (PARTITION BY Identifier,TRUNC(store.transaction_datetime, 'HH')) CNT_PER_HOUR,
TO_CHAR(TRUNC(store.transaction_datetime, 'DD'), 'DD-MON-YYYY HH24:MI:SS') DAY_TIME,
COUNT (*) OVER (PARTITION BY Identifier,TRUNC(store.transaction_datetime, 'DD')) CNT_PER_DAY,
store.transaction_datetime
FROM data.stats store
WHERE store.transaction_datetime >= '2016-09-04 00:00:00'
AND store.transaction_datetime <= '2016-09-10 23:59:59') T
ORDER BY TO_CHAR (TRUNC (T.transaction_datetime, 'HH'), 'DD-MON-YYYY HH24:MI:SS') ASC;

Oracle GROUP BY and date precision

I've got a table with two date fields : BEGIN_DATE and END_DATE
When I subtract these two fields, I get a number in days.
I want this number in seconds because the difference between these two fields is very tiny (~ 1s). So I proceed by doing :
SELECT ROUND(AVG((END_DATE-BEGIN_DATE)*3600*24),2) AS DELTA,
TO_CHAR(BEGIN_DATE, 'yyyy-mm-dd hh24:mi:ss') AS DEB,
TO_CHAR(END_DATE, 'yyyy-mm-dd hh24:mi:ss') AS FIN
FROM MYTABLE
GROUP BY TO_CHAR(BEGIN_DATE, 'yyyy-mm-dd hh24:mi:ss'),
TO_CHAR(END_DATE, 'yyyy-mm-dd hh24:mi:ss');
Here is the result (same precision with group by minutes) :
Well.
Then if I group the results by hour or by day :
SELECT ROUND(AVG((END_DATE-BEGIN_DATE)*3600*24),2) AS DELTA,
TO_CHAR(BEGIN_DATE, 'yyyy-mm-dd hh24') AS DEB,
TO_CHAR(END_DATE, 'yyyy-mm-dd hh24') AS FIN
FROM MYTABLE
GROUP BY TO_CHAR(BEGIN_DATE, 'yyyy-mm-dd hh24'),
TO_CHAR(END_DATE, 'yyyy-mm-dd hh24');
I've got this result :
The DELTA precision is better and I can't understand why !
Could someone explain me ?
My bad, don't go further, the problem is relatively simple.
Floating results are given by : SUM(DELTA) / COUNT(Grouped rows).
So if I've got 20 values at 2015-11-02 19.
9 of these values are equal to 1, remainder equals to 0.
We've got 9/20 = 0.45 which is absolutely logic.
I just verified it.
Thank you anyway for your time.

Oracle Timestamp Conversion with Dates

Assuming this has a simple solution, but I can't find it.
I'm trying to do some logic on a DATE field in Oracle. My desire is to take a DATE field and subtract X hours from it.
For instance: SELECT A.MyDATE - 100 Hours from dual;
however, I need a result in a timestamp format 'YYYY-MM-DD hh:mm'.
I've tried CAST(A.MyDATE as TIMESTAMP) - NUMTODSINTERVAL(100/24,'day') however it didn't work.
I found out that the issue is that the MyDATE field when cast to a timestamp still contained some residual time elements. How can I reset these??
Thanks!
You can just do this with subtraction:
select a.MyDate - 100.0/24
To convert to varchar:
select to_char(a.MyDate - 100.0/24, 'YYYY-MM-DD')
And, if you want to get rid of that pesky time on the date:
select trunc(a.MyDate - 100.0/24) as JustTheDate
The formats and dates in my example can be changed to any other formats and dates:
SELECT To_Timestamp(To_Char(Sysdate - INTERVAL '100' HOUR, 'MM/DD/YYYY HH24:MI'), 'MM/DD/YYYY HH24:MI')
FROM dual
/
Output:
2/4/2013 10:18:00.000000000 AM
To remove time element add Trunc() to any of your dates...:
SELECT Trunc(To_Timestamp(To_Char(Sysdate - INTERVAL '100' HOUR, 'MM/DD/YYYY HH24:MI'), 'MM/DD/YYYY HH24:MI'))
FROM dual
/
Output: 2/4/2013
Conversion/Casting - when using other dates in place of sysdate then add formats as in my other examples:
SELECT CAST(SYSDATE AS TIMESTAMP) - INTERVAL '100' HOUR FROM dual
/
Output: 2/4/2013 10:26:35.000000000 AM
SELECT start_date tstamp_to_date, CAST(start_date AS timestamp) date_to_tstamp FROM
(
SELECT to_date(to_char(to_timestamp ('2013-02-07 10:07:47.000' , 'YYYY-MM-DD HH24:MI:SS.FF'),'DD-MON-YYYY HH24:MI:SS'), 'DD-MON-YYYY HH24:MI:SS') start_date
FROM dual
)
/
Output:
tstamp_to_date date_to_tstamp
-------------------------------------------------------
2/7/2013 10:07:47 AM 2/7/2013 10:07:47.000000 AM
In Oracle, a DATE always has a day and a time component. Depending on the tool you are using and your session's NLS_DATE_FORMAT, it is entirely possible that the tool may not display the time component when you look at the data. But that is simply a display question, it has no impact on the actual data.
If you want to subtract 100 hours from midnight on the day that MyDate represents
SELECT TRUNC(MyDate) - interval '100' hour
FROM dual
This will return a DATE. If you want to return a string in a particular format
SELECT TO_CHAR( TRUNC(MyDate) - interval '100' hour, 'YYYY-MM-DD hh:mi am' )
FROM dual
Note that I'm assuming that there was a typo in your question. I assume that you want to display the minutes after the hour (mi) rather than the month (mm).
I am trying to fetch the records which is older than 30 days (from Mod_date) and I am using the below query and it is returning all the data and I want only 30 days old data.
Sample :- Mod_date 03-NOV-12 12.00.00.000000000 AM
Query :-
select Mod_date from fil_cnfact where Mod_date <= sysdate -30 order by Mod_date asc ;