title: SQL Find the first non null value with arbitrary order by
body: <p>Given the following table:</p>
  
  <pre><code>ID | Value
  ----------
   1 | NULL
   2 |  B
   3 |  C
   4 | NULL
  </code></pre>
  
  <p>I want to get the first non-null value from the Value column according to different ordering, e.g.:</p>
  
  <pre><code>SELECT FIRST_NON_NULL(Value ORDER BY ID) FROM MY_TABLE
  </code></pre>
  
  <p>This will return B</p>
  
  <pre><code>SELECT FIRST_NON_NULL(Value ORDER BY ID DESC) FROM MY_TABLE
  </code></pre>
  
  <p>This will return C</p>
  
  <p>P.S. Doesn't have to be a value function, just the SELECT statement that is needed. Thank you.</p>
  
  <hr>
  
  <p>Edited: Want to see if there can be a scalable version.</p>
  
  <pre><code>ID | Value1 | Value2
  --------------------
   1 |  NULL  |   AA
   2 |   B    |  NULL
   3 |   C    |   CC
   4 |  NULL  |  NULL
  
  SELECT FIRST_NON_NULL(Value1, Value2 ORDER BY ID) FROM MY_TABLE
  </code></pre>
  
  <p>This will return B, AA</p>
  
  <pre><code>SELECT FIRST_NON_NULL(Value1, Value2 ORDER BY ID DESC) FROM MY_TABLE
  </code></pre>
  
  <p>This will return C, CC</p>