jwcolby
jwcolby at colbyconsulting.com
Thu Oct 11 14:08:46 CDT 2012
One way would be to put a FK in the record which holds the PK of the previously stored record for a given stock. That requires looking up the previous record before updating the new record but it then gives you a dead simple inner join to get the difference between the current and the previous. Just join the table on itself using the PK and that FK. It is called a "single linked list" when done in code. John W. Colby Colby Consulting Reality is what refuses to go away when you do not believe in it On 10/11/2012 1:15 PM, Philippe lelynx wrote: > Hi all, > > I need your help regarding a sql query. > > I have a table, say STOCK(stock_date, stock_qty) > > It records the stock level at the end of each day. > > I need to calculate, for each day, the stock variation. > > i.e for the day n => stock(n) - stock(n-1) > > How would you build such a query?? > > Regards, > > Philippe Pons >