title: Query with hierarchical structure and variable number of hierarchy levels
body: <p>I have table 'Areas' containing customer's areas:</p>
  
  <pre><code>ID, AREA_NAME, PARENT_ID
  </code></pre>
  
  <p>Areas are organized in hierarchy up to four levels using PARENT_ID pointing to parent's AREA_ID. PARENT_ID for top level Area IS NULL.</p>
  
  <p>Table of 'Customers' hase AREA_ID referencing the lowest level of area hierarchy:</p>
  
  <pre><code>ID, CUSTOMER_NAME, AREA_ID
  </code></pre>
  
  <p>I want to get the result table like this:</p>
  
  <pre><code>CUSTOMER_NAME,AREA_LVL_1,AREA_LVL_2,AREA_LVL_3,AREA_LVL_4
  </code></pre>
  
  <p>The problem here is that customer's area (AREA_ID) does not allways point to the lowest fourth level of areas. Sometimes it points to fourth level AREA_ID, sometimes third and so on.</p>
  
  <p>Depending of area levels, the resulting table should look like:</p>
  
  <pre><code>CUSTOMER_NAME | AREA_LVL_1 | AREA_LVL_2 | AREA_LVL_3 | AREA_LVL_4
  ==============+============+============+============+===========
  John          | A          | A1         | A13        | A136
  Maria         | B          | B2         | &lt;null&gt;     | &lt;null&gt;
  Steve         | A          | A2         | A24        | &lt;null&gt;
  </code></pre>
  
  <p>I don't know how to make this recursion with variable number of levels inside single SQL query.
  I need just single SQL query (not procedure with cursor passing the recursion loop).</p>