0

I have set of vehicle information in SQL table with their Start date and Failed date.

I need to find the vehicle current condition is Running or not.

Example Table as follow,

Vehicle_ID status date_on
1 Start 2018-05-23
1 Trouble 2019-05-23
1 Parked 2020-06-23
1 Start 2021-06-15
1 Failed 2020-08-10
2 Start 2019-06-23
3 Start 2010-04-20
3 Parked 2011-04-20
3 Failed 2012-05-10
4 Start 2011-01-20
4 Failed 2015-01-14
4 Start 2016-02-25

Above table says,Vehicle ID 1 latest date_on value is 2021-06-15 and their status is start, so it means Vehicle is still running.

Vehicle ID 2 has only one record and that latest date_on value is 2019-06-23 and their status is start, so it means Vehicle is still running.

Vehicle ID 3 latest date_on value is '2010-05-10' and their status is Failed, so it means Vehicle is stopped.

Required Output

VehicleID Running?
1 Yes
2 Yes
3 No
4 Yes

Note: In status column has more than 5 different state and dates but we consider only Start and Failed Status

Rock
  • 27
  • 7

3 Answers3

1
select Vehicle_ID, case when status = 'Start' then 'YES' else 'NO' end as 'Running?'
from Yourtable y1
where date_on = ( 
                 select max(date_on) 
                 from Yourtable 
                 where Vehicle_ID = y1.Vehicle_ID  
                 group by Vehicle_ID)
TimLer
  • 1,320
  • 2
  • 16
  • Thanks for your immediate suggestions. Now I made few changes in above example table. In that table status column has more than 5 different status and dates but we consider only Start and Failed Status to define running or not. I need you help to re modify the above your SQL statement – Rock Aug 31 '21 at 10:21
  • You can not change your question whenever you want and expect us to do your job. You have my answer and you can develop it. – TimLer Aug 31 '21 at 10:28
  • Thanks for your comments. I made few changes on your statement, after that the statement working fine without issue – Rock Sep 01 '21 at 08:15
0

You can try the below

with cte as
(
select Vehicle_ID,status,
row_number() over(partition by Vehicle_ID order by date_on desc) as rn
from tablename
)

select Vehicle_ID, case when status='Start' then 'Yes' else 'No' end as Running
from cte 
where rn=1
Fahmi
  • 37,315
  • 5
  • 22
  • 31
  • Why not use `FETCH FIRST`? – jarlh Aug 31 '21 at 09:36
  • Thanks for your immediate suggestions. Now I made few changes in above example table. In that table status column has more than 5 different status and dates but we consider only Start and Failed Status to define running or not. I need you help to re modify the above your SQL statement – Rock Aug 31 '21 at 10:23
  • 2
    @jarlh That only gets you one row, this is one row per group. If you mean `TOP 1 WITH TIES... ORDER BY rn` that is less efficient – Charlieface Aug 31 '21 at 10:30
  • @Charlieface, great point! (Seems like I didn't read the question carefully enough...) – jarlh Aug 31 '21 at 10:33
0

You can use aggregation with conditional logic:

select vehicle_id,
       (case when max(date_on) = max(case when status = 'Start' then date_on end)
             then 'Yes' else 'No'
        end) as is_running
from t
group by vehicle_id;

This checks if the last date for the vehicle is the last date for "Start".

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • your statement working fine. And also I have added status ='Start' and status ='Failed' under WHERE condition for excluding other status. many thanks to you. – Rock Sep 01 '21 at 07:56