I'm having a problem grouping a field in my query. Here is an example of what I'm talking about:
Example:
AIR_DT DOL_GAP_TIME MATL_SIZE
15-JAN-15 8:00 AM 30
15-JAN-15 8:00 AM 25
15-JAN-15 9:00 AM 5
15-JAN-15 9:00 AM 10
15-JAN-15 9:00 AM 5
15-JAN-15 9:00 AM 20
Those with same time should be grouped as one, summing up their matl_size
Expected output:
AIR_DT DOL_GAP_TIME MATL_SIZE
15-JAN-15 8:00 AM 55
15-JAN-15 9:00 AM 40
Here is my SQL:
SELECT
a.air_dt,
TRIM(SUBSTR(TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'),12,2))
|| ':00 '
|| TRIM(SUBSTR (TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'),
21, 2)) dol_gap_time, e.matl_size
FROM order_implem_dtl_broadcast a, matl_mstr b, matl_size_mstr e
WHERE a.matl_id = b.matl_id
AND b.matl_size_id = e.matl_size_id
AND a.air_dt LIKE '%15-JAN-15%'
GROUP BY a.air_dt, a.dol_pref_start_time, e.matl_size
ORDER BY a.air_dt, a.dol_pref_start_time;
Thank you for helping in advance!
Based on your sample data, this should do what you want:
select AIR_DT, DOL_GAP_TIME, sum(MATL_SIZE)
from table t
group by AIR_DT, DOL_GAP_TIME;
However, I have no idea what this has to do with the query in the question.
OK let's start with your initial query:
SELECT
a.air_dt,
TRIM(SUBSTR(TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'),12,2))
|| ':00 '
|| TRIM(SUBSTR (TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'), 21, 2)) dol_gap_time, e.matl_size
FROM order_implem_dtl_broadcast a, matl_mstr b, matl_size_mstr e
WHERE a.matl_id = b.matl_id
AND b.matl_size_id = e.matl_size_id
AND a.air_dt LIKE '%15-JAN-15%'
GROUP BY a.air_dt, a.dol_pref_start_time, e.matl_size
ORDER BY a.air_dt, a.dol_pref_start_time;
I see a couple of problems. One, you're grouping by e.matl_size even though that is the column you want to SUM(). You don't want it in the GROUP BY. Second, your manner of getting the time from dol_pref_start_time is really odd. It looks like you want to round down to the hour, then just get the hour plus whether it is AM or PM. So this:
TRIM(SUBSTR(TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'),12,2))
|| ':00 '
|| TRIM(SUBSTR (TO_CHAR (a.dol_pref_start_time, 'mm/dd/yyyy hh:mi:ss AM'), 21, 2)) dol_gap_time
can simply be this:
TO_CHAR(TRUNC(a.dol_pref_start_time, 'HH'), 'HH:MI AM') AS dol_gap_time
Third, are your dates stored as dates? If so, why are you doing this?
AND a.air_dt LIKE '%15-JAN-15%'
It would be far better to do this:
AND TRUNC(a.air_dt) = date'2015-01-15'
or, if you have an index on a.air_dt, this:
AND a.air_dt >= date'2015-01-15'
AND a.air_dt < date'2015-01-16'
Putting this all together, we get something like this (note that I've also converted your joins to ANSI SQL joins):
SELECT TRUNC(a.air_dt) AS air_dt
, TO_CHAR(TRUNC(a.dol_pref_start_time, 'HH'), 'HH:MI AM') AS dol_gap_time
, SUM(e.matl_size) AS matl_size
FROM order_implem_dtl_broadcast a INNER JOIN matl_mstr b
ON a.matl_id = b.matl_id
INNER JOIN matl_size_mstr e
ON b.matl_size_id = e.matl_size_id
WHERE a.air_dt >= date'2015-01-15'
AND a.air_dt < date'2015-01-16'
GROUP BY TRUNC(a.air_dt), TO_CHAR(TRUNC(a.dol_pref_start_time, 'HH'), 'HH:MI AM')
ORDER BY air_dt, TO_DATE(dol_gap_time, 'HH:MI AM'); -- using aliases in the ORDER BY, converting dol_gap_time to DATE for sorting
Related
Is it possible to turn the current time stamp to a whole number?
Example: If sysdate returns 1/19/2022 5:36:49 PM can I turn that to 1/19/2022 5PM since it falls in the 5PM range.
Here is my query
Select FACILITY, TRK_ID, LOT_DTTM, IN_QTY
from TRK_ID_LOT
WHERE facility in 'DP1DM5'
and trk_id like ('AE%')
and lot_dttm > sysdate - 1
EXAMPLE:
Truncate it to hours:
SQL> select trunc(to_date('1/19/2022 5:36:49 PM', 'mm/dd/yyyy hh:mi:ss pm'), 'hh') res
2 from dual;
RES
----------------------
01/19/2022 05:00:00 PM
SQL>
If you want to update rows, do so using the same function:
update your_table set
date_column = trunc(date_column, 'hh');
Trying to compare two dates with time.But comparison is not working.
SELECT *
FROM attendance
WHERE TO_DATE (checktime, 'DD/MM/YYYY HH:MI:SS AM') >=
TO_DATE ('01/09/2019 04:30:00 PM', 'DD/MM/YYYY HH:MI:SS AM')
AND TO_DATE (checktime, 'DD/MM/YYYY HH:MI:SS AM') <=
TO_DATE ('30/09/2019 10:00:00 PM', 'DD/MM/YYYY HH:MI:SS AM')
AND userid = '3825'
AND SUBSTR (checktime, -2, 2) = 'PM'
ORDER BY TO_DATE (checktime, 'DD/MM/YYYY HH:MI:SS AM') ASC
I was expecting output equal or grater then 04:30 PM and less then or equal 10:00 PM.But this date comparison is not working.Here is the
Output of Code.I want my result includes date and time between mentioned periods.
Note:CHECKTIME datatype is varchar2.
I think you need data for all the days(01/09/2019 - 30/09/2019) and the time of the day should be between 04:30 PM and 10: PM.
You can achieve this using the following query:
SELECT
*
FROM
ATTENDANCE
WHERE
TRUNC(TO_DATE(CHECKTIME, 'DD/MM/YYYY HH:MI:SS AM'))
BETWEEN TO_DATE('01/09/2019', 'DD/MM/YYYY')
AND TO_DATE('30/09/2019', 'DD/MM/YYYY')
AND ( TO_DATE(CHECKTIME, 'DD/MM/YYYY HH:MI:SS AM') - TRUNC(TO_DATE(CHECKTIME, 'DD/MM/YYYY HH:MI:SS AM')) ) * 1440 -- converting difference into minutes
BETWEEN 990 -- 04:30 PM in minutes (16.5*60)
AND 1320 -- 10:00 PM in minutes (22*60)
AND USERID = '3825'
AND SUBSTR(CHECKTIME, - 2, 2) = 'PM'
ORDER BY
TO_DATE(CHECKTIME, 'DD/MM/YYYY HH:MI:SS AM') ASC;
Cheers!!
You can fix your format by using such a format containing TO_TIMESTAMP conversion
SELECT *
FROM attendance
WHERE checktime
BETWEEN TO_TIMESTAMP('01/09/2019 16:30:00.000000','dd/mm/yyyy hh24:mi:ss.ff')
AND TO_TIMESTAMP('30/09/2019 22:30:00.000000','dd/mm/yyyy hh24:mi:ss.ff')
AND userid = 3825
ORDER BY checktime;
Demo
EDIT : you had better to add a new column with timestamp datatype, and update your new column's data by using TO_TIMESTAMP conversion such as below :
UPDATE attendance
SET checktime2 = TO_TIMESTAMP('3/09/2019 5:38:36 PM','dd/mm/yyyy hh:mi:ss AM')
WHERE id = 3825
AND checktime = '3/09/2019 5:38:36 PM'
I am trying to calculate the difference between 2 datetime values where non-work hours are ignored. Originally it just looked at the difference and calculated it as minutes however It needs to count only hours between 9am and 8pm Monday to Friday and 9am - 1pm Saturday, ignoring all other times. I am on an oracle 10g system.
my code as it currently stands is as follows:
begin
debug.debug('sp_access');
update cl_case b
set time_to_sp_access =
(
select (x.date_created-e.date_created)*1440
from cl_case c, eventlog e, eventlog x
where c.id=e.case_id
and x.case_id=e.case_id
and b.id=e.case_id
and e.id=
( select min(id) from eventlog mini
where mini.case_id=e.case_id
and mini.cl_code in ('AAAA','BBBB','CCCC','DDDD')
)
and x.id=
( select min(id) from eventlog minix
where minix.case_id=e.case_id
and minix.cl_code in ('EEEE','FFF','GGG','HHHH','JJJJ','KKKK','LLLL')
)
)
where id in
( select unique case_id
from eventlog elog
where elog.sptime_needs_setting ='Y'
);
commit;
end sp_access;
How can I get this to count time between specified hours?
thanks
You could use a CASE expression in the WHERE clause. Since there are two datetime values, you need to use two case expressions.
For example, the CASE expression would evaluate as:
SQL> SELECT
2 CASE
3 WHEN TO_CHAR(SYSDATE, 'DY') BETWEEN '1' AND '5'
4 THEN TO_DATE(TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY')
5 ||' 08:00:00 PM', 'MM/DD/YYYY HH:MI:SS PM')
6 ELSE TO_DATE(TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY')
7 ||' 01:00:00 PM', 'MM/DD/YYYY HH:MI:SS PM')
8 END my_time
9 FROM dual;
MY_TIME
----------------------
11/24/2015 01:00:00 pm
The above example check the DAY for SYSDATE, and depending on it returns a datetime value.
using the above example, since you have two different datetime values to be compared as a date range condition, you will need two CASE expressions in your WHERE clause.
WHERE date_column
BETWEEN
CASE
WHEN TO_CHAR(date_column, 'DY') BETWEEN '1' AND '5'
THEN
TO_DATE(TO_CHAR(
TRUNC(date_column), 'MM/DD/YYYY')
||' 09:00:00 AM', 'MM/DD/YYYY HH:MI:SS PM')
ELSE
TO_DATE(TO_CHAR(
TRUNC(date_column), 'MM/DD/YYYY')
||' 09:00:00 AM', 'MM/DD/YYYY HH:MI:SS PM')
END
AND
CASE
WHEN TO_CHAR(date_column, 'DY') BETWEEN '1' AND '5'
THEN
TO_DATE(TO_CHAR(
TRUNC(date_column), 'MM/DD/YYYY')
||' 08:00:00 PM', 'MM/DD/YYYY HH:MI:SS PM')
ELSE
TO_DATE(TO_CHAR(
TRUNC(date_column), 'MM/DD/YYYY')
||' 01:00:00 PM', 'MM/DD/YYYY HH:MI:SS PM')
END
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');
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