I have an external third-party program exporting a database to mysql in real time and I want to show data for reporting. Thus, I cannot change the structure because it is synchronized in real time.
The table structure looks something like this:
ID | Date | Transaction ----------------------------- 12 | 2012-11-01 | 200 12 | 2012-11-02 | 250 12 | 2012-11-03 | 150 12 | 2012-11-04 | 1000 12 | 2012-11-05 | 225 .... 13 | 2012-11-01 | 175 13 | 2012-11-02 | 20 13 | 2012-11-03 | 50 13 | 2012-11-04 | 100 13 | 2012-11-05 | 180 13 | 2012-11-06 | 195
The data is very large and continues to grow every day.
What I want to do is create a report (lookup table) based on something like this:
ID | Date | Transaction | Prev Day Transaction ---------------------------------------------------- 12 | 2012-11-01 | 200 | 0 12 | 2012-11-02 | 250 | 200 12 | 2012-11-03 | 150 | 250 12 | 2012-11-04 | 1000 | 150 12 | 2012-11-05 | 225 | 1000 .... 13 | 2012-11-01 | 175 | 0 13 | 2012-11-02 | 20 | 175 13 | 2012-11-03 | 50 | 20 13 | 2012-11-04 | 100 | 50 13 | 2012-11-05 | 180 | 100 13 | 2012-11-06 | 195 | 180
I just can't get quick pick instructions. Currently, the source data is already 283,120 rows. And it will grow like 500 rows per day.
I tried something like:
SELECT *, (SELECT transaction FROM table as t2 WHERE t1.id=t2.id AND t1.date>t2.date ORDER BY t2.date DESC LIMIT 0,1) FROM table AS t1
It works, but the select statement is very slow. Most of the time it is cut off in the middle of the operation.
I need help - this is a very fast SQL statement that I could later use to create a view table.
source share