Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please note <a href="http://www.sqlservercentral.com/Forums/FindPost415849.aspx" rel="nofollow">the difference between tamp tables, table variables and CTEs</a>.</p> <p>You are currently using <a href="http://msdn.microsoft.com/en-us/library/ms190766.aspx" rel="nofollow">Common Table Expressions</a>, or CTEs, which are more akin to <a href="http://www.4guysfromrolla.com/webtech/112098-1.shtml" rel="nofollow">derived tables</a>, as they can be used for only one select (or insert, update, etc.) statement that directly follows them (though you can declare more than one CTE for a statement, as you have done).</p> <p>As such, by the time you get to your second select statement, your CTEs have gone out of scope, and you can no longer refer to them (or you have to declare them again). You may want to use a <a href="http://odetocode.com/articles/365.aspx" rel="nofollow">table variable</a> or a <a href="http://www.codeproject.com/Articles/42553/Quick-Overview-Temporary-Tables-in-SQL-Server-2005" rel="nofollow">temp table</a> in this case.</p> <p>You might easily create a temp table by converting your CTE definitions like the following:</p> <pre><code>-- Make a temp table SELECT * INTO #TempTableName -- Create a new temp table FROM AVL WHERE DATE_TIME between '01/30/2013 00:00:00' AND '01/30/2013 23:59:59' AND radio_name = 'MAS7' -- Check out our new temp table SELECT * FROM #TempTableName -- Clean up our temp table, though this will automatically happen once the connection is dropped DROP TABLE #TempTableName </code></pre> <p>Also, when working with temp tables, I'll often check for and drop them at the beginning and end of my procs with something like the following:</p> <pre><code>if object_id('tempdb..#TempTableName') is not null begin drop table #TempTableName end </code></pre> <p>With this stuff in your code, please let us know if you have difficulty performing the actual distance calculations.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload