Search code examples
sqloracle-databasemaxdateadd

Max and add month for SQL not working


select maintance_action_date,add_months(maintenance_action_date,3) 
as Service_By 
from maintenance 
where bike_no = 1;

So far this query shows me all the times that bike has undergone maintenance and then adds 3 months to it. However I want it to display the most recent date and add three months to it. Anyone know how I would do this.


Solution

  • You could use max() function for get the recent date

    select max(maintance_action_date) ,add_months(max(maintenance_action_date),3) 
    as Service_By 
    from maintenance 
    where bike_no = 1;
    

    or Try using an intermediary select

    select my_date, add_months(t.my_date,3) 
    from ( 
        select max(maintance_action_date)  my_date 
        from maintenance 
        where bike_no = 1
        ) t