title: SQL - combine two functions to one table
body: <p>So I have the first code which I use: </p>
  
  <pre><code>  with idlist as (
  Select uniqueid as masterid
  From table1
  Union
  Select uniqueid 
  From table2 
  )
  
  Select i.masterid,
  t1.*,
  t2.*
  From idlist as i
  Left join table1 on t1.uniqueid = i.masterid
  Left join table2 on t2.uniqueid = i.masterid
  </code></pre>
  
  <p>Purpose of the code above: to take 2 or more tables which have the same id column and union to one row.</p>
  
  <p>Second code: </p>
  
  <pre><code>select [Id], [price], [description]
  from [table1]
  where name_1 % 10 = 8  -- enter name_1 manually
  union all
  select [Id], [price], [description]
  from [table2]
  where name_2 % 10 = 8
  </code></pre>
  
  <p>Purpose of the code above: check a specific column, if it ends with '8' then list its Id, price, description</p>
  
  <p><strong>What I want</strong>:
  Basically combine those two codes. I need the second code to run <strong>within</strong> the result of the first code.
  I thought of creating a new table for the result of the first query above, but as far as i know i need to create all the columns before inserting data into it, yet the result of the first query will be a combination of several tables which have a lot of different columns. 
  Nonetheless, i still want the result of the combo between the two queries to be in a new table.</p>
  
  <p>So if had to put everything to words and steps: 
  Select id from tables> union under the same Id to one row with all their row content > create table with the result > run the second code above on the new table</p>
  
  <p>Thanks</p>