Report the last event (notice) for a device

I could have sworn that I had this work before, but for the life of me I don't remember how I did it and I can't seem to understand this new.

I am tracking events by device. A device can have any number of events in a given period of time. I need follow the MOST RECENT event for a device by device. I don't want to have multiple records by device, only the record with the MOST RECENT timestamp.

I made two subsequent attempts, but I still get nowhere.
select
  device,
  MAX(dt) KEEP (DENSE_RANK FIRST ORDER BY dt) as time_Var
from db_table partition (pPname)
where
    Lower(example) like '%example text%'
    and dt > sysdate - 5
and
select DISTINCT
device,
       (select TO_CHAR( (MAX(table_name.dt)), 'HH24:MI:SS MM/DD/YYYY') "timeVar"
          from table_name partition (pPName)
          where
       Lower(Example) like '%example text%'
       and dt > sysdate - 5)
          
from table_name partition (pPName)
where
    Lower(Example) like '%example text%'
    and dt > sysdate - 5
order by device Asc;

Hello

Savage solution with

KEEP (DENSE_RANK FIRST ORDER BY dt)

will show you the event the less recent in the last 5 days.
If you want the most recent event, change "FIRST", "Last".

KEEP (DENSE_RANK LAST ORDER BY dt)

or sort in descending order:

KEEP (DENSE_RANK FIRST ORDER BY dt DESC)

If you want to display more than one column in the last row, you will need to use the DUNGEON (DENSE_RANK...) on each of them (except devices).
In this case, you will find may be easier to use an analytical function, such as ROW_NUMBER:

WITH     ranked_data  AS
(
     SELECT     device, column_a, column_b, column_c
     ,     ROW_NUMBER () OVER ( PARTITION BY  device
                               ORDER BY          dt     DESC
                       )  AS r_num
     FROM     your_table
     WHERE     LOWER (example)      LIKE '%example text%'
     AND     dt               > SYSDATE - 5
)
SELECT  device, column_a, column_b, column_c
FROM     ranked_data
WHERE     r_num   = 1;

Tags: Database

Similar Questions

Maybe you are looking for