﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Needed Temp Table for View / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 17:54:19 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Needed Temp Table for View</title><link>http://www.sqlservercentral.com/Forums/Topic1379972-392-1.aspx</link><description>The decision to use a cte or a subquery boils down to preference. This type of query has been presented both ways time and time again with tons and tons of performance evaluation and the two will almost always produce identical execution plans. There truly is no performance between the two, just use whichever version is easier for you to use/understand.</description><pubDate>Mon, 05 Nov 2012 07:18:43 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Needed Temp Table for View</title><link>http://www.sqlservercentral.com/Forums/Topic1379972-392-1.aspx</link><description>I've come to use CTE's rather than subqueries, as in the last example, simply because it is easier for me to read from the top down than from inside out.</description><pubDate>Sun, 04 Nov 2012 21:19:51 GMT</pubDate><dc:creator>The Dixie Flatline</dc:creator></item><item><title>RE: Needed Temp Table for View</title><link>http://www.sqlservercentral.com/Forums/Topic1379972-392-1.aspx</link><description>You could use a simpler method than cte: subquery. This basic technique exists from sql2000 (and probably even earlier)and is worth to be learned:[code="sql"]SELECT *FROM (	SELECT A.COMPKEY, B.HYDR_ID, B.DATE, B.hydr_gpm, B.gpm_20, B.gpm_10, B.gpm_0, B.staff_id, B.shift, 		B.static, B.residual, B.pitot, B.pitot2, coeff, A.Shape, C.not_in_srv, A.FLOWTEST, 		Rank() over (Partition BY B.HYDR_ID order by B.DTTM DESC) AS RANK			FROM GISOWNER.WATHYDRANT A			LEFT JOIN S_4.FH.dbo.hyd_actv B				ON A.COMPKEY = B.HYDR_ID			LEFT JOIN S_4.FH.dbo.hyd_main C				ON A.COMPKEY = C.HYDR_ID	WHERE A.FIREZONE IN ('FZ1', 'FZ2', 'FZ3', 'FZ4', 'FZ5', 'FZ6', 'FZ7') AND (B.FLOWTEST = '1' OR C.not_in_srv = '1')) FLOWTEST WHERE RANK = 1[/code]CTE is required only if you want to use a subquery more than once, or in a recursion.</description><pubDate>Fri, 02 Nov 2012 19:26:42 GMT</pubDate><dc:creator>Vedran Kesegic</dc:creator></item><item><title>RE: Needed Temp Table for View</title><link>http://www.sqlservercentral.com/Forums/Topic1379972-392-1.aspx</link><description>You're welcome. Glad that worked for you.</description><pubDate>Thu, 01 Nov 2012 13:37:59 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Needed Temp Table for View</title><link>http://www.sqlservercentral.com/Forums/Topic1379972-392-1.aspx</link><description>That worked perfectly!  I always forget about CTEs.Thanks!!!!</description><pubDate>Thu, 01 Nov 2012 13:35:58 GMT</pubDate><dc:creator>jordon.shaw</dc:creator></item><item><title>RE: Needed Temp Table for View</title><link>http://www.sqlservercentral.com/Forums/Topic1379972-392-1.aspx</link><description>You can use a cte in a view so this should work for you.[code];with FLOWTEST as(	SELECT A.COMPKEY, B.HYDR_ID, B.DATE, B.hydr_gpm, B.gpm_20, B.gpm_10, B.gpm_0, B.staff_id, B.shift, 		B.static, B.residual, B.pitot, B.pitot2, coeff, A.Shape, C.not_in_srv, A.FLOWTEST, 		Rank() over (Partition BY B.HYDR_ID order by B.DTTM DESC) AS RANK			FROM GISOWNER.WATHYDRANT A			LEFT JOIN S_4.FH.dbo.hyd_actv B				ON A.COMPKEY = B.HYDR_ID			LEFT JOIN S_4.FH.dbo.hyd_main C				ON A.COMPKEY = C.HYDR_ID	WHERE A.FIREZONE IN ('FZ1', 'FZ2', 'FZ3', 'FZ4', 'FZ5', 'FZ6', 'FZ7') AND (B.FLOWTEST = '1' OR C.not_in_srv = '1'))	SELECT * FROM FLOWTEST WHERE RANK = 1[/code]</description><pubDate>Thu, 01 Nov 2012 12:50:42 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>Needed Temp Table for View</title><link>http://www.sqlservercentral.com/Forums/Topic1379972-392-1.aspx</link><description>I have the need to create a view for one of my applications to look at.  The problem is, when I write the query to get the data I need, the only way I know how to do it is by using a temp table; however, I know that temp tables are not allowed in view.  Is there anyway to write the below script in a manner that I could put it in a view without using temp tables?[code="sql"]  SELECT A.COMPKEY, B.HYDR_ID, B.DATE, B.hydr_gpm, B.gpm_20, B.gpm_10, B.gpm_0, B.staff_id, B.shift, 		B.static, B.residual, B.pitot, B.pitot2, coeff, A.Shape, C.not_in_srv, A.FLOWTEST, 		Rank() over (Partition BY B.HYDR_ID order by B.DTTM DESC) AS RANK	INTO #FLOWTEST		FROM GISOWNER.WATHYDRANT A			LEFT JOIN S_4.FH.dbo.hyd_actv B				ON A.COMPKEY = B.HYDR_ID			LEFT JOIN S_4.FH.dbo.hyd_main C				ON A.COMPKEY = C.HYDR_ID	WHERE A.FIREZONE IN ('FZ1', 'FZ2', 'FZ3', 'FZ4', 'FZ5', 'FZ6', 'FZ7') AND (B.FLOWTEST = '1' OR C.not_in_srv = '1')	SELECT * FROM #FLOWTEST WHERE RANK = '1'	DROP TABLE #FLOWTEST[/code]Thank you for any help that you can give!Jordon</description><pubDate>Thu, 01 Nov 2012 12:32:00 GMT</pubDate><dc:creator>jordon.shaw</dc:creator></item></channel></rss>