I need to subtract two rows in MySQL by using the most recent date from the previous date:
Starting table:
Stock Date Price
GOOG 2012-05-20 402
GOOG 2012-05-21 432
APPL 2012-05-20 553
APPL 2012-05-21 590
FB 2012-05-20 40
FB 2012-05-21 34
This is the result table:
Stock Date Price Change
GOOG 2012-05-21 30
APPL 2012-05-21 37
FB 2012-05-21 -6
Right now I just have two dates per company, but bonus upvotes if your query can handle any number of dates.
What I’ve done was I get two separate queries to get each stock’s maximum date and minimum date. Try this,
SQLFiddle Demo
UPDATE 1
seeing your last sentence:
Right now I just have two dates per company, but bonus upvotes if your query can handle any number of dates.What if you have records like this?what would be its result?