title: Issue Joining SQL table with statement containing Row Number Aggregate Function
body: <p>Using SQL in excel, I have created a statement to pull and join records using the following post as a guide 
  <a href="https://stackoverflow.com/questions/16802001/sql-2008-combining-data-from-multiple-rows-with-the-same-id-into-one-row">SQL 2008 Combining data from multiple rows with the same ID into one row</a> (First Answer)
  I am not able to figure out how to join an additional table to it now. I think I am just adding it to the wrong area.</p>
  
  <p>Here is the working code:</p>
  
  <pre><code>sql = sql &amp; "select REPID, ReportDate, "
  sql = sql &amp; "max(case when CategoryID = 5001 then Score end) InPercent, "
  sql = sql &amp; "max(case when CategoryID = 5002 then Score end) OutPercent "
  sql = sql &amp; "from "
  sql = sql &amp; "(select REPID, ReportDate, Score, CategoryID, row_number() over(partition by REPID, ReportDate order by Score) rn from REPREPORTING RR Where RepID &lt;&gt; 0) "
  sql = sql &amp; " d group by REPID, ReportDate "
  </code></pre>
  
  <p>And here is the join statement I need to add:</p>
  
  <pre><code>sql = sql &amp; "JOIN EMPCUR_EmployeeCurr P ON P.employee_token_nr = RR.REPID "
  </code></pre>
  
  <p>I normally would put this at the end of the statement, and have tried that and get the error Incorrect syntax near the word join. I have also placed it prior to the group by line with the same error.</p>
  
  <p>Any assistance is appreciated!</p>