Oracle group by minute - sql

I have written a query to get the data received with respect to every hour in a day.
SELECT To_CHAR( A.req_start_time , 'DD/MM/YYYY HH24') as input , count(A.REQUEST_ID)
FROM ILBULK.SAS_RE_TASK_MESSAGE A,ILBULK.SAS_RE_REQUEST_MESSAGE
WHERE A.NE_TYPE = 'HLR'
LIKE '%Synchronous%'
AND A.REQUEST_ID = ILBULK.SAS_RE_REQUEST_MESSAGE.REQUEST_ID and A.REQ_START_TIME > to_DATE ('12/26/2014 00', 'MM/DD/YYYY HH24') and A.REQ_START_TIME < to_DATE('12/27/2014 00', 'MM/DD/YYYY HH24')
GROUP BY To_CHAR(A.REQ_START_TIME, 'DD/MM/YYYY HH24');
And I am getting following response
26/12/2014 02 13823
26/12/2014 14 4681
26/12/2014 12 2939
26/12/2014 18 457
26/12/2014 03 34327
26/12/2014 04 15673
26/12/2014 19 28885
26/12/2014 06 70699
26/12/2014 10 10743
Now i want to get data with respect to every minute ordered in ascending order, I have tried to split the hours but nothings working. How do I do that?

Use to_char(A.req_start_time , 'DD/MM/YYYY HH24:MI'), where MI is the minutes part. Add the same to the SELECT as well as GROUP BY clause.
SELECT To_char(A.req_start_time, 'DD/MM/YYYY HH24:MI') AS input,
Count(A.request_id)
FROM ilbulk.sas_re_task_message A,
ilbulk.sas_re_request_message
WHERE A.ne_type = 'HLR' LIKE '%Synchronous%'
AND A.request_id = ilbulk.sas_re_request_message.request_id
AND A.req_start_time > To_date ('12/26/2014 00', 'MM/DD/YYYY HH24')
AND A.req_start_time < To_date('12/27/2014 00', 'MM/DD/YYYY HH24')
GROUP BY To_char(A.req_start_time, 'DD/MM/YYYY HH24:MI');
For example,
SQL> SELECT to_char(SYSDATE, 'DD/MM/YYYY HH24:MI') GRP_MIN
2 FROM DUAL
3 GROUP BY to_char(SYSDATE, 'DD/MM/YYYY HH24:MI')
4 /
GRP_MIN
----------------
29/12/2014 11:50
SQL>

Your query doesn't appear to be including minutes in any of the date/time strings. You could try the following pattern:
'DD/MM/YYYY HH24:MI' - where MI represents minutes
For ordering purposes, you will need to convert your input field back to a date:
ORDER BY to_date(input, 'DD/MM/YYYY HH24:MI')
If you use the date pattern YYYY/MM/DD HH24:MI, conversion wouldn't be necessary as the Strings would come out in the same order as date/time. In Oracle, ascending sort is assumed but you can add ASC at the end for clarity if you prefer.
If you are after minutes since midnight, you will need something like this:
TO_NUMBER(TO_CHAR(input, 'SSSSS'))/60
In the above, SSSSS represents seconds since midnight - divide by 60 for minutes.

I have figured it out myself. :) Here is the new query
SELECT To_CHAR( A.req_start_time , 'HH24:MI:SS') as input , count(A.REQUEST_ID)
FROM ILBULK.SAS_RE_TASK_MESSAGE A,ILBULK.SAS_RE_REQUEST_MESSAGE
WHERE A.NE_TYPE = 'HLR'
--and ILBULK.SAS_RE_REQUEST_MESSAGE.PROTOCOL LIKE '%SAS%' --and ILINK.SAS_RE_REQUEST_MESSAGE.PROTOCOL LIKE '%Synchronous%'
AND A.REQUEST_ID = ILBULK.SAS_RE_REQUEST_MESSAGE.REQUEST_ID and A.REQ_START_TIME > to_DATE ('12/26/2014 00', 'MM/DD/YYYY HH24') and A.REQ_START_TIME < to_DATE('12/27/2014 00', 'MM/DD/YYYY HH24')
GROUP BY To_CHAR(A.REQ_START_TIME, 'HH24:MI:SS');

Related

Group by singles chute

How can I create an overview without duplicates in column "CHUTE"?
Eg in below result: AX002 = 129
select
COUNT(PPL_SDCC),
substr (PPL_DISCHARGEID,6,5) as CHUTE
from T1LOG.PPL_PIECELOG
where
PPL_DISCHARGEID like 'PS%X%'
and substr (PPL_SDCC,11,1)='1'
and PPL_DISCHARGETIME between TO_DATE ('01/08/2021 10:00:00' , 'DD/MM/YYYY hh24:mi:ss') and TO_DATE ('02/08/2021 09:00:00' , 'DD/MM/YYYY hh24:mi:ss')
group by PPL_DISCHARGEID
order by CHUTE
RESULT:
Rahther than grouping only column PPL_DISCHARGEID, You should actually include the exact calculation in group by clause. Please update your group by clause in your query to -
SELECT COUNT(PPL_SDCC),
SUBSTR(PPL_DISCHARGEID,6,5) as CHUTE
FROM T1LOG.PPL_PIECELOG
WHERE PPL_DISCHARGEID like 'PS%X%'
AND SUBSTR(PPL_SDCC,11,1)='1'
AND PPL_DISCHARGETIME BETWEEN TO_DATE('01/08/2021 10:00:00', 'DD/MM/YYYY hh24:mi:ss') and TO_DATE('02/08/2021 09:00:00', 'DD/MM/YYYY hh24:mi:ss')
GROUP BY SUBSTR(PPL_DISCHARGEID,6,5)
ORDER BY CHUTE;

ORACLE get count time range from both date

I'm using ORACLE, how to get time duration from this below date:
SELECT 24 * (TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI') - TO_CHAR(LASTACTIVITY, 'YYYY-MM-DD HH24:MI')) diff_hours
FROM WA_GA_TBL_USERS
I got error:
ORA-01722: invalid number
What I want is to get count time range from both date.
Example:
SYSDATE = 2017-10-06 17:00
LASTACTIVITY = 2017-10-06 15:30
And the result is: 1.5
And for next I will validate it with PHP
if($timeduration > 1) //1 means 1 hour
{
}
You already have date expressions, you don't need to convert them to varchars:
SELECT 24 * (SYSDATE - LASTACTIVITY) diff_hours FROM WA_GA_TBL_USERS
select
24 * (to_date('2017-10-06 17:00', 'YYYY-MM-DD hh24:mi') - to_date('2017-10-06 15:30', 'YYYY-MM-DD hh24:mi')) as diff_hours
from dual;
Usually i use php with mysql but i don't think the semantic is different the , i didn't try the code below but can be an help for you :
<?php
$c = oci_pconnect(...);
$s = oci_parse($c, "select 24 * (to_date('2017-10-06 17:00', 'YYYY-MM-DD hh24:mi') - to_date('2017-10-06 15:30', 'YYYY-MM-DD hh24:mi')) as diff_hours from dual");
oci_execute($s);
$res=oci_fetch_array($s);
if($res >1)
//..
?>

Get the data with a between condition

I have query
select * from TABLE_NAME
where date
between to_date('26/01/2016', 'dd/mm/yyyy hh24:mi')
and to_date('26/01/2016', 'dd/mm/yyyy hh24:mi');
01/26/2016 11:59:32 PM
above one is the date which need to fetch when running the query
But when i running the same query it was not able to filter. Don't know why?
I don't know where i am doing wrong.
between to_date('26/01/2016', 'dd/mm/yyyy hh24:mi') and to_date('26/01/2016', 'dd/mm/yyyy hh24:mi')
You do not have any time portion in to_date('26/01/2016', 'dd/mm/yyyy hh24:mi') at all. You only have date portion, so the filter will ignore the time portion and return all the rows for that date. The time portion in your query is:
SQL> alter session set nls_date_format='DD/MM/YYYY HH24:MI:SS';
Session altered.
SQL> SELECT to_date('26/01/2016', 'dd/mm/yyyy hh24:mi') FROM dual;
TO_DATE('26/01/2016
-------------------
26/01/2016 00:00:00
To filter the rows with 01/26/2016 11:59:32 PM you need:
TO_DATE('01/26/2016 11:59:32 PM' ,'DD/MM/YYYY HH:MI:SS PM')

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.

start/end date and time in oracle sql query

I need a query output like the below table;
This is a primary entry to a table and these records will be modified by a third party program which I have no control. Can anyone suggest a good sample?
ID | DATEIN | DATEOUT | STATUS
1 02.02.2014 00:00:00 02.02.2014 23:59:59 1
2 03.02.2014 00:00:00 03.02.2014 23:59:59 0
I tried
SELECT To_Char(To_Date(SYSDATE), 'dd-MM-yyyy hh:mm:ss PM'),
To_Char(date_add(To_Date(SYSDATE +1), INTERVAL -1 SECOND), 'dd-MM-yyyy hh:mm:ss PM')
FROM dual
but this query throws an error ORA-00907: missing right parenthesis.
There is no need for PM if you want it to be in 24-hour format. And pay attention to the mask for minutes, it is mi, not mm as in your query. Also as already mentioned no need to convert SYSDATE to date as it is already of that datatype:
SELECT to_char(to_date(SYSDATE), 'dd-mm-yyyy HH24:mi:ss') date_in,
to_char(to_date(SYSDATE + 1) - INTERVAL '1' SECOND, 'dd-mm-yyyy HH24:mi:ss') date_out
FROM dual;
DATE_IN DATE_OUT
------------------- -------------------
11-03-2014 00:00:00 11-03-2014 23:59:59
You can do away with DATE_ADD and TO_DATE functions (SYSDATE is already a DATE, no need of conversion ) , and also use mi to show minute instead of mm which is format specifier for month as in:
SELECT To_Char(SYSDATE, 'dd-MM-yyyy hh:mi:ss PM'),
To_Char((SYSDATE + 1) + INTERVAL '-1' SECOND, 'dd-MM-yyyy hh:mi:ss PM')
FROM dual
I am not clear what you are trying to achieve from the above query but if parenthesis is your only problem then you gotta hit the query:
SELECT To_Char(To_Date((SYSDATE), 'dd-MM-yyyy hh:mm:ss PM')),
To_Char(date_add(To_Date(SYSDATE +1), INTERVAL -1 SECOND), 'dd-MM-yyyy hh:mm:ss PM')
FROM dual