I have a table full of prices, items, and dates. An example of this is:
AA, 1/2/3024, 1.22
AA, 1/3/3024, 1.23
BB, 1/2/3024, 4.22
BB, 1/3/3024, 4.23
Within the data there are only two rows per price, and they are ordered by date. How would I condense this data set into a single product row showing the difference from the last price to the previous? [Also this applies to a ratio, so AA would produce 1.23/1.22].
The result should look like
AA, todays price-yesterdays price
Despite being a sum function, there is no subtraction function over a list.
I'm using postgres 9.1.
SQLFiddle example: http://sqlfiddle.com/#!15/9f7d6/1
Since
there are only two rows per price
, this can be much simpler and faster:->SQLfiddle
This form carries the additional benefit, that you can use all columns from both rows directly.
For more complex scenarios (not necessary for this), you could use window functions as has been pointed out. Here is a simpler approach than what has been suggested:
Only one window function is needed here. And one query level, since
DISTINCT ON
is applied after window functions. The sort order in the window agrees with overall sort order, helping performance.If there's only two rows per item, then
will do the trick.