title: SQL Server : column prefix does not match with a table name
body: <p>Here is my query. I keep getting the same error </p>
  
  <blockquote>
    <p>The column prefix 'A' does not match with a table name or alias name used in the query.</p>
  </blockquote>
  
  <p>although the query seems right to me </p>
  
  <pre><code>Declare @date1 nvarchar(50) = '2015-12-31'
  Declare @date2 nvarchar(50) = '2016-03-31'
  
  DECLARE @query AS NVARCHAR(max) = 
       N'SELECT financial_element_name, financial_structure_value, [' + @date2 + '] , [' + @date1 + ']
         FROM (SELECT A.[financial_structure_value], A.financial_structure_id,
                      A.financial_analysis_session_id, A.period
               FROM counterparty_financial_structure A
               INNER JOIN financial_analysis_session FAS ON FAS.financial_analysis_session_id = A.financial_analysis_session_id
               WHERE A.counterparty_id = 8736) AS Source_table
          PIVOT  
               (max(A.[financial_structure_value]) for A.period in (['+@date2+'] , ['+@date1+']) ) AS PivotTable;'
  
  EXEC (@query)
  </code></pre>
  
  <p>Any suggestions?</p>