How to update sale item price based on last time it was sold

Like the title says, I would like to update the sale price of an item based on the last sold price.
I guess this can be done with a script? How should I do that?