title: SQL Get Rows with given value or rows that are the closest to the value
body: <p>I need to get from a query the rows with the value equal to a parameter or the one that are the closed based on ID.</p>
  
  <pre><code>ID          Value
  ----------- -----------
  1           1
  1           2
  1           3
  2           1
  2           2
  3           1
  
  CREATE TABLE #Test (ID int, Value int)
  
  INSERT INTO #Test VALUES (1,1)
  INSERT INTO #Test VALUES (1,2)
  INSERT INTO #Test VALUES (1,3)
  INSERT INTO #Test VALUES (2,1)
  INSERT INTO #Test VALUES (2,2)
  INSERT INTO #Test VALUES (3,1) 
  </code></pre>
  
  <p>So for a value = 3 it should return </p>
  
  <pre><code>ID          Value
  ----------- -----------
  1           3
  2           2
  3           1
  </code></pre>
  
  <p>Becouse for ID = 1 we have Value 3, and for the other I want the value that is the closest to my given input 3. </p>
  
  <p>Edit: </p>
  
  <p>So for value = 2 it should return </p>
  
  <pre><code>ID          Value
  ----------- -----------
  1           2
  2           2
  3           1
  </code></pre>