Reputation: 1950
I have a table with following date and value. I need to subtract the values based on the date[max date value - min date value] and create a table.
date value
2014-11-07 229275
2014-11-24 138746
2014-12-17 127112
The intended output is:
maxdate mindate value
2014-12-17 2014-11-07 102163
Upvotes: 0
Views: 519
Reputation: 1269773
If you could use max(value) and min(value), then it would be easy:
select max(date) as maxdate, min(date) as mindate, max(value) - min(value)
from table;
This happens to work for your example.
Assuming you really want the value on the min date and on the max date, try this instead:
select maxt.date as maxdate, mint.date as mindate, (mint.value - maxt.value) as diff
from (select t.* from table t order by date limit 1) as mint cross join
(select t.* from table t order by date desc limit 1) as maxt;
Upvotes: 1