title: Redshift : Based on whether column exists in table_1, create another table and assign column values
body: <p>I have a table in Redshift named 'table_1', with columns named 'column_name_1','column_name_2','column_name_3'.......'column_m' etc. </p>
  
  <pre><code>table_1
  column_name_1   column_name_2   column_name_3   column_name_4   ....   column_name_m    
  </code></pre>
  
  <p>I want to make a temporary table named 'temp_table' based on the following conditions:</p>
  
  <ol>
  <li>Select the 3 columns named 
  <code>'column_name_1','column_name_2','column_name_3'</code></li>
  </ol>
  
  <p>which I am sure already exist in 'table_1'.</p>
  
  <ol start="2">
  <li>Select column named 'column_n' which I am not sure exists in 'table_1'. If 'column_n' exists in 'table_1', select it as it is, if it does not exist, then make a column in 'temp_table' named 'column_n' and assign it a character value 'val_n' all the rows.</li>
  </ol>
  
  <p>So my final 'temp_table' will look like this:</p>
  
  <ol>
  <li>If 'column_n' exists in 'table_1'</li>
  </ol>
  
  <p><code>temp_table</code></p>
  
  <pre><code>column_name_1  column_name_2   column_name_3   column_n
  col_1_val_1    col_2_val_1     col_3_val_1     col_n_val_1
  col_1_val_2    col_2_val_2     col_3_val_2     col_n_val_2
  col_1_val_3    col_2_val_3     col_3_val_3     col_n_val_3
  ....           ....            ....            ....   
  </code></pre>
  
  <ol start="2">
  <li>If 'column_n' does not exist in 'table_1'</li>
  </ol>
  
  <p><code>temp_table</code></p>
  
  <pre><code>column_name_1  column_name_2   column_name_3   column_n
  col_1_val_1    col_2_val_1     col_3_val_1     val_n
  col_1_val_2    col_2_val_2     col_3_val_2     val_n
  col_1_val_3    col_2_val_3     col_3_val_3     val_n
  ....           ....            ....            ....   
  </code></pre>
  
  <p>I tried to take some hints from this link : <a href="https://stackoverflow.com/questions/9991043/how-can-i-test-if-a-column-exists-in-a-table-using-an-sql-statement">How can I test if a column exists in a table using an SQL statement</a>, but somehow I have not been successful in testing for a condition inside creation of a temporary table.</p>