0

I have a PostgreSQL table that have these values:

dollar_rate; end_date
------
1. 46.77; 2015-09-16 11:03:03.4082
2. 46.70; 2015-09-17 11:27:00.2446
3. 46.67; 2015-09-17 15:09:15.0354

Now I have this query:

  SELECT dollar_value,end_date-now() from dollar_rate;

The results:

1. 46.77; -04:48:34.945704
2. 46.70; 19:35:21.890696
3. 46.67; 23:17:36.681496

As you can see at the third row, I would like to select this row, that is, the row that contains the highest value with the computation end_date-now(). How can I do this in PostgreSQL?

2

Sort the result (descending) by the value of end_date - now() and limit to only one row:

SELECT dollar_value, end_date - now() 
from dollar_rate
order by end_date - now() desc
limit 1;

The computation end_date - now() is actually unnecessary in this case, you can simplify this to:

SELECT dollar_value, end_date - now() 
from dollar_rate
order by end_date desc 
limit 1;
  • Ah, thank you. Thank you for reminding me the benefits of sorting with a "limit" keyword. – Allan Registos Sep 16 '15 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.