title: select single row from multiple rows by id
body: <p>I have a database table like the following.</p>
  
  <pre><code>  id |  value  |flag
     2    null     1
     2     z       2
     3     x       1
     3     y       2
     3     z       3
     4     null    2
  </code></pre>
  
  <p>I need result like the following</p>
  
  <pre><code>   id |  value  |flag
     2     z       2
     3     x       1
     4     null     2
  </code></pre>
  
  <p>If the value is null, the value from other matched id should be taken (2-z-2). If all the rows of an id are not null then the value should be taken by priority which is 1 here (3-x-1). If an id has only null value, take any single  row  with null value and flag does not matter here.</p>