title: SQL Group by query - choose row in priority
body: <p>I work in Oracle database, where I need create query for following table (result of query).</p>
  
  <pre><code>CUSTOMER_REFENRECE LQ   UN   SPECIAL INSTRUCTIONS
  ------------------ ---- ---- --------------------------------
  FI020098           null null Not Restricted as per ADR SP 375
  FI020098           HZ   1219 HZ UN1219
  FI020098           LQ   1219 UN1219
  FI020097           null null null
  FI020097           LQ   1219 UN1219
  </code></pre>
  
  <p>I need get just one row per <code>CUSTOMER_REFERENCE</code> in order to:</p>
  
  <p><strong>HZ > LQ > NULL</strong></p>
  
  <p><strong>If there is one HZ, then just return HZ row (skip null)</strong></p>
  
  <pre><code>FI020098 - HZ- 1219- HZ UN1219
  </code></pre>
  
  <p><strong>If there isnt any HZ and at least one LQ, return row with LQ</strong></p>
  
  <pre><code>FI020097 - LQ - 1219 - UN1219
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/mYyMH.png" rel="nofollow noreferrer">Table</a></p>