title: How to create an algorithm with SQL Server
body: <p>I am new on this platform and I want to know if I can create an algorithm using your help and advises.</p>
  
  <p>I received this project where I need to find the new price and the last price for different agreements, taking into consideration the availability of the these contracts together with the quantity sold (which can be scaled) since the version of SAP does not have any audit history of user actions.</p>
  
  <p>I managed to filter the pairs of two contracts but this represents only 20% of this project. Now I would like to create an algorithm that can filter the agreements renewed with different quantities sold at different prices, on the same/different time period. The business scenario for this part is to look for the 2 closest numbers for all cases when there are 2 different arrays or same array, for different types of date intervals. The tools that I have to work with are SQL Server Management Studio and Alteryx designer. Do you think you can help me?</p>
  
  <p>An example of the table I have to work with is the below (but there are thousands of keys):</p>
  
  <p><img src="https://i.stack.imgur.com/uJWap.png" alt="image"></p>