Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Needed Temp Table for View Expand / Collapse
Author
Message
Posted Thursday, November 1, 2012 12:32 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, November 5, 2014 7:53 AM
Points: 151, Visits: 215
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
Post #1379972
Posted Thursday, November 1, 2012 12:50 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:10 AM
Points: 13,230, Visits: 12,709
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 Moden's 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)
Post #1379979
Posted Thursday, November 1, 2012 1:35 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, November 5, 2014 7:53 AM
Points: 151, Visits: 215
That worked perfectly! I always forget about CTEs.

Thanks!!!!
Post #1380002
Posted Thursday, November 1, 2012 1:37 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:10 AM
Points: 13,230, Visits: 12,709
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 Moden's 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)
Post #1380005
Posted Friday, November 2, 2012 7:26 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, September 29, 2014 5:52 AM
Points: 369, Visits: 1,215
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
Post #1380656
Posted Sunday, November 4, 2012 9:19 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 2,787, Visits: 6,098
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? -- Stephen Stills
Post #1380864
Posted Monday, November 5, 2012 7:18 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:10 AM
Points: 13,230, Visits: 12,709
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 Moden's 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)
Post #1381072
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse