title: TSQL aggregate functions to populate temp tables
body: <p>I want to run a T-SQL script where I create a temp table that will be aggregated by a certain field of another table, call it table X. The remaining fields of this temp table will be populated by performing aggregate functions on the fields of table X. Then I would like to do a MERGE / WHEN MATCHED with my temp table on a different table (call it table Y) after I have populated the temp table.</p>
  
  <p>How do I create this temp table and populate it with aggregate functions? (I have already coded the MERGE part of the problem).</p>