Here is the original question: There is a table stockprices, which contains information about trades of one company's stock. It has two columns: timestamp and price. They represent the time when a trade happened and the price of the sale. The natural order of records in the table is random and is not sorted by timestamp.
You add another column (let's call it delta) where you intend to store the difference between the current transaction price and the price of the previous transaction time-wise.
Write a single SQL statement, which will calculate the price difference and fill the column delta with it in all rows.
I write the sql as below to run on mysql, but seems current is not supported on mysql, and i can't move on, the following is the one i wrote till now to calculate the difference between the current transaction price and the price of the previous transaction time-wise:
SELECT
[current].timestamp,
[current].price,
ISNULL([next].price, 0) - [current].price
FROM
stockprice AS [current]
LEFT JOIN
stockprice AS [next]
ON [next].timestamp = (SELECT MIN(timestamp) FROM stockprice WHERE timestamp > [current].timestamp)