title: Msg 8156, Level 16, State 1, Line 17 The column 'BID_Close' was specified multiple times for 't'
body: <p>I already found a problem for this on StackOverflow link:-
  <a href="https://stackoverflow.com/questions/41737767/sql-server-2008-specified-column-was-specified-multiple-times">SQL Server 2008 Specified column was specified multiple times</a> but it doesn't help me much.</p>
  
  <p>The below code throws an exception. How can I fix this?</p>
  
  <pre><code>--Calculating the average true range---------
  WITH cteRange AS 
  (
      SELECT 
          t.[Date],
          ROUND(t.[BID_High] - t.[BID_Low], 5) AS [H-L],
          ABS(ROUND(t.[BID_High] - t.[BID_Close], 5)) AS [H-Cp],
          ABS(ROUND(t.[BID_Low] - t.[BID_Close], 5)) AS [L-Cp]
      FROM 
          (SELECT 
               *, 
               [BID_Close]  = LAG([BID_Close]) OVER (ORDER BY [Date])
           FROM 
               [STOCK_DATA_FOR_ANALYSIS]) AS t 
  ), cteTrueRange AS 
  (
      SELECT *
      FROM cteRange
      CROSS APPLY (
          SELECT MAX(v) AS v
          FROM (VALUES ([H-L]), ([H-Cp]), ([L-Cp])) AS value(v)
      ) AS sub([TR])
  ), cteTrueRange10 AS 
  (
      SELECT
          *,
          LAG([TR], 1) OVER (ORDER BY [Date]) AS [TR1],
          LAG([TR], 2) OVER (ORDER BY [Date]) AS [TR2],
          LAG([TR], 3) OVER (ORDER BY [Date]) AS [TR3],
          LAG([TR], 4) OVER (ORDER BY [Date]) AS [TR4],
          LAG([TR], 5) OVER (ORDER BY [Date]) AS [TR5],
          LAG([TR], 6) OVER (ORDER BY [Date]) AS [TR6],
          LAG([TR], 7) OVER (ORDER BY [Date]) AS [TR7],
          LAG([TR], 8) OVER (ORDER BY [Date]) AS [TR8],
          LAG([TR], 9) OVER (ORDER BY [Date]) AS [TR9]
      FROM cteTrueRange
  )
  SELECT 
      [Date], [H-L], [H-Cp], [L-Cp], [TR], [TRA]
  FROM 
      cteTrueRange10
  CROSS APPLY 
      (SELECT CASE WHEN [TR9] IS NOT NULL THEN AVG(v) END AS v
          FROM (VALUES ([TR]), ([TR1]), ([TR2]), ([TR3]), ([TR4]), ([TR5]), ([TR6]), ([TR7]), ([TR8]), ([TR9])) AS value(v)
      ) AS sub([TRA]);
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>Msg 8156, Level 16, State 1, Line 17<br>
    The column 'BID_Close' was specified multiple times for 't'.</p>
  </blockquote>