title: How to pass the output of select query as column list to another select query in postgresql?
body: <p>I'm trying to pass the output of a select query as column list to another select query.</p>
  
  <pre><code>select ARRAY_TO_STRING(ARRAY(SELECT distinct COLUMN_NAME::VARCHAR(50)
  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='products' AND 
  COLUMN_NAME NOT IN ('code') ), ', ');
  </code></pre>
  
  <p>The above query will give the output as like give below,</p>
  
  <pre class="lang-none prettyprint-override"><code>array_to_string      
  ---------------------------  
  name, price, manufacturer
  </code></pre>
  
  <p>Now I need to pass this output as column list to another select query. So I tried like this,</p>
  
  <pre><code>select ( select ARRAY_TO_STRING(ARRAY(SELECT distinct
  COLUMN_NAME::VARCHAR(50) FROM INFORMATION_SCHEMA.COLUMNS WHERE
  TABLE_NAME='products' AND  COLUMN_NAME NOT IN ('code') ), ', ') ) 
  from products;
  </code></pre>
  
  <p>But it is not working properly. It gives the output like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>          ?column?          
   ---------------------------  
   name, price, manufacturer  
   name, price, manufacturer  
   name, price, manufacturer
  </code></pre>
  
  <p>Instead of,</p>
  
  <pre class="lang-none prettyprint-override"><code>       name       | price | manufacturer 
   -----------------+-------+--------------  
   Hard drive       |   240 |            5  
   Memory           |   120 |            6  
   ZIP drive        |   150 |            4
  </code></pre>
  
  <p>So, how can I solve this?</p>