SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Needed Temp Table for View


Needed Temp Table for View

Author
Message
jordon.shaw
jordon.shaw
SSC Veteran
SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)

Group: General Forum Members
Points: 296 Visits: 224
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?


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



Thank you for any help that you can give!

Jordon
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25996 Visits: 17528
You can use a cte in a view so this should work for you.


;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



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
jordon.shaw
jordon.shaw
SSC Veteran
SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)

Group: General Forum Members
Points: 296 Visits: 224
That worked perfectly! I always forget about CTEs.

Thanks!!!!
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25996 Visits: 17528
You're welcome. Glad that worked for you.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Vedran Kesegic
Vedran Kesegic
Say Hey Kid
Say Hey Kid (710 reputation)Say Hey Kid (710 reputation)Say Hey Kid (710 reputation)Say Hey Kid (710 reputation)Say Hey Kid (710 reputation)Say Hey Kid (710 reputation)Say Hey Kid (710 reputation)Say Hey Kid (710 reputation)

Group: General Forum Members
Points: 710 Visits: 1266
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:

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



CTE is required only if you want to use a subquery more than once, or in a recursion.

_____________________________________________________
Microsoft Certified Master: SQL Server 2008
XDetails Addin - for SQL Developers
blog.sqlxdetails.com - Transaction log myths

The Dixie Flatline
The Dixie Flatline
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5390 Visits: 6900
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.

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25996 Visits: 17528
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.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search