Skip to content Skip to sidebar Skip to footer

Postgres - Having Group By Min Of Date Time And Status

I would like to retrieve car_id from the below table which have min (svc_date) with status=A . expected is 103 and 100 which have status 'A' because it have the status A with

Solution 1:

Really, it is hard to understand to you:

with cte as (
select 
  car_id ,
  status,
  rank() OVER (
     PARTITIONBY car_id 
     ORDERBY svc_date )       
from car_tbl
)
select*from cte 
where rank =1and status ='A'

Results:

| CAR_ID | STATUS | RANK |
--------------------------
|    100 |      A |    1 |
|    103 |      A |    1 |

Solution 2:

It's rather difficult to see what you're really asking; but does this do what you want?

SELECT car_id, status, MIN(svc_date) FROM car_tbl WHERE status ='A'GROUPBY car_id, status

Post a Comment for "Postgres - Having Group By Min Of Date Time And Status"