SQL Order in two columns - sql

I am going nuts trying to fix these small issues. I'm trying to have the DATE_TIME be in order and want the MISTI to be in order. The screenshot shows the MISTI in order but not the dates. Is there any way I can remove the 2019 date out of the query too? THANK YOU IN ADVANCE!
select To_Char(state_out_dttm, 'MM/DD/YYYY HH24:MI:SS') AS date_time, PARM, TRK_ID as MISTI,
num_value AS PM_NUMBER
from trk_id_parm
Where trk_id IN ('IMV01','IMV02','IMV03', 'IMV04', 'IMV05')
and state = 'SMM'
and PARM = '4440'
order by MISTI ASC, DATE_TIME DESC;

As it appears that your state_out_dttm column is actually text, and not a date, you should be using TO_DATE, not TO_CHAR, to first convert to a bona fide date before trying to sort:
SELECT
TO_DATE(state_out_dttm, 'MM/DD/YYYY HH24:MI:SS') AS date_time,
PARM,
TRK_ID AS MISTI,
num_value AS PM_NUMBER
FROM trk_id_parm
WHERE
trk_id IN ('IMV01','IMV02','IMV03', 'IMV04', 'IMV05') AND
state = 'SMM' AND
PARM = '4440' AND
EXTRACT(YEAR FROM TO_DATE(state_out_dttm, 'MM/DD/YYYY HH24:MI:SS')) <> 2019
ORDER BY
MISTI,
TO_DATE(state_out_dttm, 'MM/DD/YYYY HH24:MI:SS') DESC;

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

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;

Data for the last date not included by the "BETWEEN OPERATOR" IN ORACLE

I have an oracle query for getting the values between 12pm and 12am ( each day) from dates 11/06/2021 to 16/06/2021. The query i have below will collect the info from the 11th up to the 15th , but will not obtain data for the 16th. Is there any other way i can solve this issue? Thanks a lot for your time
SELECT TO_CHAR(datadate, 'dd/mm/yyyy') ,
ROUND(AVG(BATTERYVOLTAGE),2) AVGVOLTAGE
FROM MYTABLE
WHERE BATTERYVOLTAGE!= 0
AND to_char(datadate, 'hh24:mi:ss') BETWEEN '12:00:00' and '24:00:00'
AND DATADATE BETWEEN TO_DATE('11/06/2021', 'dd/mm/yyyy')
AND TO_DATE('16/06/2021', 'dd/mm/yyyy')
GROUP BY TO_CHAR(datadate, 'dd/mm/yyyy')
ORDER BY 1
datadate BETWEEN to_date('11/06/2021', 'dd/mm/yyyy')
AND to_date('16/06/2021', 'dd/mm/yyyy')
includes only rows where datadate if before or at 2021-06-16 00:00:00. Everything past 2021-06-16 00:00:00 isn't included.
That's a common mistake. Don't use BETWEEN for such things. Use a range with >= and < instead.
datadate >= to_date('2021-06-11 12:00:00', 'YYYY-MM-DD HH24:MI:SS')
AND datadate < to_date('2021-06-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
If anyone wondering how to do it.
Change the formatting of the last date.
DATADATE BETWEEN TO_DATE('12/06/2021', 'dd/mm/yyyy') AND (TO_DATE('22/06/2021', 'dd/mm/yyyy')+(1-1/24/60/60))
For more info
https://asktom.oracle.com/pls/apex/asktom.search?tag=date-queries-using-between
You will have problems try to query date and time separated; is easier if you join date and time and search for datetime value like to_date('2021-06-11 09:00:00','YYYY-MM-DD HH24:MI:SS') the range you'll get is since 9 at the limit you want

Select a date within a year in SQL Oracle

Given:
INSERT INTO EP_ACCESS (PROFILE_ID, EPISODE_ID, START_TIMESTAMP, DISCONNECT_TIMESTAMP)
VALUES ('1', '1', TO_DATE('2020-01-01 00:00:01','yyyy/mm/dd hh24:mi:ss'), TO_DATE('2020-01-01 00:00:02','yyyy/mm/dd hh24:mi:ss'));
How can I select those who start_timestamp is in 2020?
You would use:
where start_timestamp >= date '2020-01-01' and
start_timestamp < date '2021-01-01'
Of course, you can use a timestamp literal if you prefer typing longer strings.
There are several options.
1 - Use BETWEEN
SELECT *
FROM EP_ACCESS
WHERE START_TIMESTAMP BETWEEN TO_DATE('2020-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
AND TO_DATE('2020-12-31 23:59:59', 'YYYY-MM-DD HH24:MI:SS')
or
SELECT *
FROM EP_ACCESS
WHERE START_TIMESTAMP BETWEEN DATE '2020-01-01'
AND DATE '2021-01-01' - INTERVAL '1' SECOND
2 - Use EXTRACT
SELECT *
FROM EP_ACCESS
WHERE EXTRACT(YEAR FROM START_TIMESTAMP) = 2020
3 - Use TRUNC
SELECT *
FROM EP_ACCESS
WHERE TRUNC(START_TIMESTAMP, 'YYYY') = DATE '2020-01-01'
Of these options, BETWEEN will probably provide the best performance as the other two require executing a function against the START_TIMESTAMP field in every row in the table.

How to retrieve rows between hours

I want to get all rows from my table, the table have one column with the following date type '14/07/2017 05:01:35 p.m.' between specific hours. Like between '00:01:00 am' and '01:00:00 am'.
I'm running Oracle Database 11g Release 2
select ID, CREATION_TIME
from my_table
where ID = 4 and
CREATION_TIME between to_date('29/04/2017 12:01:00', 'DD/MM/YYYY HH:MI:SS[AM]') and to_date('29/04/2019 01:00:00', 'DD/MM/YYYY HH:MI:SS[AM]')
order by creation_time asc;
I want to select hours instead of dates.
If you mean (from your last comment) that you want to get all records that are between the dates of Jan 1st 2019 and Apr 4th 2019, but only if the time portion of the recod is between 12:01AM and 1:00AM, then you could try this:
select ID, CREATION_TIME
from my_table
where ID = 4
and CREATION_TIME between to_date('01/01/2017', 'DD/MM/YYYY')
and to_date('04/04/2019', 'DD/MM/YYYY')
and CREATION_TIME BETWEEN TO_DATE(TO_CHAR(CREATION_TIME, 'MM/DD/YYYY') || ' 00:01:00', 'MM/DD/YYYY HH24:MI:SS')
AND
TO_DATE(TO_CHAR(CREATION_TIME, 'MM/DD/YYYY') || ' 01:00:00', 'MM/DD/YYYY HH24:MI:SS')
order by creation_time asc;