title: Going back in time to see if a value exists in SQL Server
body: <p>I have a need to create a query in SQL that will take a record set (the select statement) and return the last value for price change that happened. so something like this scenario.</p>
  
  <pre><code>--Create a TableVariable(only in memory)
  Declare @CarPrices Table (testid int, car nvarchar(50),price 
  nvarchar(50),PricingDate date)
  --Insert Data
  insert into @CarPrices (testid, car, price, PricingDate) values (1, 'Ford', 
  '1.00043', '05/15/2018')
  insert into @CarPrices (testid, car, price, PricingDate) values (2, 
  'Chevy','1.00043', '05/15/2018')
  insert into @CarPrices (testid, car, price, PricingDate) values (3, 'Chevy', 
  NULL, '05/16/2018')
  insert into @CarPrices (testid, car, price, PricingDate) values (4, 'Ford', 
  NULL, '05/16/2018')
  insert into @CarPrices (testid, car, price, PricingDate) values (5, 'Ford', 
  '1.0053', '05/17/2018')
  insert into @CarPrices (testid, car, price, PricingDate) values (6, 'Chevy', 
  NULL, '05/17/2018')
  insert into @CarPrices (testid, car, price, PricingDate) values (7, 'Chevy', 
  NULL, '05/18/2018')
  insert into @CarPrices (testid, car, price, PricingDate) values (8, 'Ford', 
  NULL, '05/18/2018')
  insert into @CarPrices (testid, car, price, PricingDate) values (9, 'Audi', 
  '10.0003', '05/18/2018')
  </code></pre>
  
  <p>Objective: Look through the data set and get the most current car date and price</p>
  
  <p>Any thoughts or anything to try. I don't have a data set to work with that is small enough so I'm using cars and pricing and dates which in essence is what I will be doing on a larger scale. Any thoughts? Maybe a complex case statement of an IIF statement? I really don't know where to start on this one.</p>