Help with recursive SQL instruction
Posted on 2008-10-23
On a SQLExpress db I have a table with the following structure:
ItemNo Date PriceSource Price
In a few words, we get prices for our items from different sources. The problem is that not all sources provide prices everyday and I need to fill the gap on one of them. What I need to do, is to copy the lowest price at any given date to PriceSource 1 if and only if PrisceSource 1 is empty.
Example: on day 31/09/2008 I get the following prices:
ItemNo, Date, PriceSource, Price
55, 31/09/2008, 2, 202.50
55, 31/09/2008, 4, 206.25
55, 31/09/2008, 5, 201.95
I have no price from PriceSource 1. Therefore I need to insert a record as follows:
55, 31/09/2008, 1, 201.95 (the lowest price above)
The code should do this for all records (dates) in the table.
Is it possible to achieve the above with a routine I could execute with SQLCMD?
Thanks a lot!