How to fetch status column based on max/min of a timestamp column

ajax

I have a table like this

Emplid REQUEST_ID Status Status_Dttm
1234 1 Open 02-Jun-2022 12.35.00.AM
1231 5 Draft 02-Jun-2022 12.30.00.AM
1234 1 In Progress 02-Jun-2022 12.47.00.AM

How can I get request date as Min(Status Dttm) and current status as Max(Status Dttm) of every request

I tried using this SELECT EMPLID,REQUEST_ID,MIN(Status_Dttm) AS "REQUEST DATE", (SELECT STATUS FROM STS_TBL WHERE MAX(Status_Dttm)) AS "CURRENT_STATUS" FROM STS_TBL;

getting error as "Group function is not allowed here"

MT0

You want to add GROUP BY request_id if you want to group by each request:

SELECT MIN(emplid) AS emplid,
       request_id,
       MIN(status_dttm) AS request_date,
       MAX(status) KEEP (DENSE_RANK LAST ORDER BY status_dttm) AS current_status 
FROM   sts_tbl
GROUP BY request_id

Which outputs:

EMPLID REQUEST_ID REQUEST_DATE CURRENT_STATUS
1234 1 2022-06-02 00:35:00 In Progress
1231 5 2022-06-02 00:30:00 Draft

If the request_id is the same for each emplid (which appears to be the case for your sample data) then you could use:

SELECT emplid,
       MIN(status_dttm) AS request_date,
       MAX(status) KEEP (DENSE_RANK LAST ORDER BY status_dttm) AS current_status 
FROM   sts_tbl
GROUP BY emplid

Which outputs:

EMPLID REQUEST_DATE CURRENT_STATUS
1231 2022-06-02 00:30:00 Draft
1234 2022-06-02 00:35:00 In Progress

db<>fiddle here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In SQL(postgresql) How to group based on a "timestamp without time zone" column?

How to fetch all values of a column based on value of another column in same table?

BigQuery partition expiration for TIMESTAMP column based partition

How to modify a timestamp column?

How to fetch a column header if a particular condition is met based on row and column value of the dataframe?

How to fetch a document based on a value of the particular column using QueryBatcher?

Fetch column value based on dynamic input

How to calculate difference in DATE based on status of another column?

SQL query to fetch data based on column value

Delete rows based on timestamp AND text in a separate column

How to fetch rows based on CRM modifiedon column

How to fetch all column values based on email?

Sum Column of Integers Based on Timestamp in PostgreSQL

SQL query to fetch rows based on a column

Add hours to timestamp in Netezza based on other column

How to fetch column names runtime

how to fetch rows in sql server based on values of column

finding nearest occurrence of timestamp in a column based on conditions

Any quick way of updating a column based on timestamp

How to create a new column with status based on value

How I fetch values from one column based on distinct values from other column?

how to fetch data from table based on column value and group by its name and count column value

Fetch single value for a record based on a condition on column

FaunaDB: how to fetch a custom column

How to apply .timestamp attribute to a column?

How can I group rows in dask based on a timestamp column and a name column whilst maintaining a certain distribution?

How to union many dataframes based on column order with casting to timestamp

How can I create a TIMESTAMP column in HIVE with a string based timestamp?

Postgresql: How to convert a timestamp column to a timestamp with time zone column at UTC?