title: Postgres FROM query with one of the column name
body: <p>As follow-up to the previous question:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/40383958/count-matches-between-multiple-columns-and-words-in-a-nested-array">Count matches between multiple columns and words in a nested array</a></li>
  </ul>
  
  <p>I have the following query:</p>
  
  <pre><code>SELECT row_number() OVER (ORDER BY t.id) AS id
       , t.id AS "RID"
       , count(DISTINCT a.ord) AS "Matches"
  FROM   tbl t
  LEFT   JOIN (
     unnest(array_content) WITH ORDINALITY x(elem, ord)
     CROSS JOIN LATERAL
     unnest(string_to_array(elem, ',')) txt
     ) a ON t.description ~ a.txt
         OR t.additional_info ~ a.txt
  GROUP  BY t.id;
  </code></pre>
  
  <p>which gives me matches correctly, but now the value for <code>array_content</code> needs to be dynamic and that too one of the column value.</p>
  
  <p>Lets say I am using a aggregator function to get the array content within the query as:</p>
  
  <pre><code>SELECT row_number() OVER (ORDER BY t.id) AS id
       , t.id AS "RID"
       , array_agg(DISTINCT demo_a.element_demo) as array_values
       , count(DISTINCT a.ord) AS "Matches"
       , count(DISTINCT demo_a.ord) AS "Demo_Matches"
  FROM   tbl t
  LEFT   JOIN (
     unnest(array_values) WITH ORDINALITY x(elem, ord)
     CROSS JOIN LATERAL
     unnest(string_to_array(elem, ',')) txt
     ) a ON t.description ~ a.txt
         OR t.additional_info ~ a.txt
  LEFT JOIN (
     unnest("test1","test2"::varchar[]) WITH ORDINALITY x(element_demo, ord)
     CROSS JOIN LATERAL
     unnest(string_to_array(element_demo, ',')) text
     ) demo_a ON i.name ~ demo_a.text
  GROUP  BY t.id;
  </code></pre>
  
  <p>Now what I need is to get the <code>array_values</code> column in place of array_content that is defined in the unnest portion. Is it possible?
  For now it is giving an exception that column name not defined.</p>