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 12»»

Create view with no outer joins Expand / Collapse
Author
Message
Posted Sunday, August 26, 2012 4:40 PM


SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Friday, August 8, 2014 11:44 AM
Points: 133, Visits: 282
Need to create view with no outer joins so I can index the view. Here is the query I use to create the view. Does anyone no of a better way?


SELECT c.cts_app_fkey AS APP_NUMBER, a.CREDIT_AMOUNT, REPLACE(d.DLR_REFERENCE_NUM, '.', '') AS DEALER_NUM, c.cts_contract_number AS APP_CONTRACT_NUMBER,
CASE co.CO_UD_APP_STATUS_TBDESC WHEN 'PO Issued' THEN CASE a.APP_DECISION_CODE WHEN 7 THEN 'Booked' ELSE 'PO Issued' END WHEN 'Approved' THEN
CASE co.CO_LESSOR WHEN '421' THEN 'Approved - SFP' WHEN '423' THEN 'Approved - SFP' WHEN '424' THEN 'Approved - SFP' ELSE 'Approved' END ELSE co.CO_UD_APP_STATUS_TBDESC
END AS APPLICATION_STATUS, CONVERT(VARCHAR, co.CO_BOOKING_DATE, 101) AS BOOKING_DATE, CONVERT(XML, a.APP_DATA,
0 ).value('(//CUST.NAME/node())[1]', 'VARCHAR(50)') AS CUSTOMER, CONVERT(XML, a.APP_DATA, 0 ).value('(//DBA/node())[1]', 'VARCHAR(50)') AS CUSTOMER_DBA,
CASE WHEN LEN(CONVERT(XML, a.APP_DATA, 0 ).value('(//CUST.CREDIT.ACCT/node())[1]', 'VARCHAR(50)')) < 8 THEN CONVERT(XML, a.APP_DATA,
0 ).value('(//CUST.CREDIT.ACCT/node())[1]', 'VARCHAR(50)') ELSE NULL END AS CCAN, CONVERT(VARCHAR, CONVERT(DATE, CONVERT(XML, a.APP_DATA,
0 ).value('(//UD_DATE_ENTERED/node())[1]', 'VARCHAR(10)')), 101) AS DATE_ENTERED, CONVERT(XML, a.APP_DATA, 0 ).value('(//FED.ID/node())[1]', 'VARCHAR(50)')
AS FEDERAL_ID
FROM dbo.APPLICATION AS a LEFT OUTER JOIN
dbo.RPT_CO AS co ON a.APP_KEY = co.APP_FKEY LEFT OUTER JOIN
dbo.DEALER AS d ON CONVERT(XML, a.APP_DATA, 0 ).value('(//DEALER/node())[1]', 'VARCHAR(50)') = d.DLR_KEY LEFT OUTER JOIN
dbo.Contract_Setup AS c on a.app_key = c.cts_app_fkey
WHERE (DATEDIFF(MINUTE, c.CTS_LAST_SAVED_DATE, GETDATE()) < 50) AND (DATEDIFF(MINUTE, c.CTS_LAST_SAVED_DATE, GETDATE()) > 30)
Post #1350163
Posted Monday, August 27, 2012 8:03 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:41 PM
Points: 13,315, Visits: 12,182
jdbrown239 (8/26/2012)
Need to create view with no outer joins so I can index the view. Here is the query I use to create the view. Does anyone no of a better way?


SELECT c.cts_app_fkey AS APP_NUMBER, a.CREDIT_AMOUNT, REPLACE(d.DLR_REFERENCE_NUM, '.', '') AS DEALER_NUM, c.cts_contract_number AS APP_CONTRACT_NUMBER,
CASE co.CO_UD_APP_STATUS_TBDESC WHEN 'PO Issued' THEN CASE a.APP_DECISION_CODE WHEN 7 THEN 'Booked' ELSE 'PO Issued' END WHEN 'Approved' THEN
CASE co.CO_LESSOR WHEN '421' THEN 'Approved - SFP' WHEN '423' THEN 'Approved - SFP' WHEN '424' THEN 'Approved - SFP' ELSE 'Approved' END ELSE co.CO_UD_APP_STATUS_TBDESC
END AS APPLICATION_STATUS, CONVERT(VARCHAR, co.CO_BOOKING_DATE, 101) AS BOOKING_DATE, CONVERT(XML, a.APP_DATA,
0 ).value('(//CUST.NAME/node())[1]', 'VARCHAR(50)') AS CUSTOMER, CONVERT(XML, a.APP_DATA, 0 ).value('(//DBA/node())[1]', 'VARCHAR(50)') AS CUSTOMER_DBA,
CASE WHEN LEN(CONVERT(XML, a.APP_DATA, 0 ).value('(//CUST.CREDIT.ACCT/node())[1]', 'VARCHAR(50)')) < 8 THEN CONVERT(XML, a.APP_DATA,
0 ).value('(//CUST.CREDIT.ACCT/node())[1]', 'VARCHAR(50)') ELSE NULL END AS CCAN, CONVERT(VARCHAR, CONVERT(DATE, CONVERT(XML, a.APP_DATA,
0 ).value('(//UD_DATE_ENTERED/node())[1]', 'VARCHAR(10)')), 101) AS DATE_ENTERED, CONVERT(XML, a.APP_DATA, 0 ).value('(//FED.ID/node())[1]', 'VARCHAR(50)')
AS FEDERAL_ID
FROM dbo.APPLICATION AS a LEFT OUTER JOIN
dbo.RPT_CO AS co ON a.APP_KEY = co.APP_FKEY LEFT OUTER JOIN
dbo.DEALER AS d ON CONVERT(XML, a.APP_DATA, 0 ).value('(//DEALER/node())[1]', 'VARCHAR(50)') = d.DLR_KEY LEFT OUTER JOIN
dbo.Contract_Setup AS c on a.app_key = c.cts_app_fkey
WHERE (DATEDIFF(MINUTE, c.CTS_LAST_SAVED_DATE, GETDATE()) < 50) AND (DATEDIFF(MINUTE, c.CTS_LAST_SAVED_DATE, GETDATE()) > 30)


Nobody knows what your tables look like or your data looks like. We also don't know the requirements. There is absolutely no way anybody has chance at answering your question unless you provide some details. Take a look at the first link in my signature about best practices when posting questions.

I took the liberty of formatting your query so people can read it. If you use the IFCode shortcuts (over on the left when posting), you can wrap your code in these blocks and it will maintain the formatting.

SELECT c.cts_app_fkey AS APP_NUMBER
,a.CREDIT_AMOUNT
,REPLACE(d.DLR_REFERENCE_NUM, '.', '') AS DEALER_NUM
,c.cts_contract_number AS APP_CONTRACT_NUMBER
,CASE co.CO_UD_APP_STATUS_TBDESC
WHEN 'PO Issued'
THEN CASE a.APP_DECISION_CODE
WHEN 7
THEN 'Booked'
ELSE 'PO Issued'
END
WHEN 'Approved'
THEN CASE co.CO_LESSOR
WHEN '421'
THEN 'Approved - SFP'
WHEN '423'
THEN 'Approved - SFP'
WHEN '424'
THEN 'Approved - SFP'
ELSE 'Approved'
END
ELSE co.CO_UD_APP_STATUS_TBDESC
END AS APPLICATION_STATUS
,CONVERT(VARCHAR, co.CO_BOOKING_DATE, 101) AS BOOKING_DATE
,CONVERT(XML, a.APP_DATA, 0).value('(//CUST.NAME/node())[1]', 'VARCHAR(50)') AS CUSTOMER
,CONVERT(XML, a.APP_DATA, 0).value('(//DBA/node())[1]', 'VARCHAR(50)') AS CUSTOMER_DBA
,CASE
WHEN LEN(CONVERT(XML, a.APP_DATA, 0).value('(//CUST.CREDIT.ACCT/node())[1]', 'VARCHAR(50)')) < 8
THEN CONVERT(XML, a.APP_DATA, 0).value('(//CUST.CREDIT.ACCT/node())[1]', 'VARCHAR(50)')
ELSE NULL
END AS CCAN
,CONVERT(VARCHAR, CONVERT(DATE, CONVERT(XML, a.APP_DATA, 0).value('(//UD_DATE_ENTERED/node())[1]', 'VARCHAR(10)')), 101) AS DATE_ENTERED
,CONVERT(XML, a.APP_DATA, 0).value('(//FED.ID/node())[1]', 'VARCHAR(50)') AS FEDERAL_ID
FROM dbo.APPLICATION AS a
LEFT JOIN dbo.RPT_CO AS co ON a.APP_KEY = co.APP_FKEY
LEFT JOIN dbo.DEALER AS d ON CONVERT(XML, a.APP_DATA, 0).value('(//DEALER/node())[1]', 'VARCHAR(50)') = d.DLR_KEY
LEFT JOIN dbo.Contract_Setup AS c ON a.app_key = c.cts_app_fkey
WHERE (DATEDIFF(MINUTE, c.CTS_LAST_SAVED_DATE, GETDATE()) < 50)
AND (DATEDIFF(MINUTE, c.CTS_LAST_SAVED_DATE, GETDATE()) > 30)



_______________________________________________________________

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 #1350400
Posted Monday, August 27, 2012 8:30 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 8:15 PM
Points: 3,564, Visits: 7,702
jdbrown239 (8/26/2012)
Need to create view with no outer joins so I can index the view.


I can't find a restriction for indexed views that involves outer joins http://msdn.microsoft.com/en-us/library/ms191432.aspx. Are you sure that's the reason? and that's what you need?
OUTER JOINS have a very specific function and can't be really replaced by something else without changing the results (OUTER APPLY is similar but I don't believe it'll solve your problem).



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1350422
Posted Monday, August 27, 2012 8:37 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 8:07 PM
Points: 12,910, Visits: 32,028
an indexed view cannot have functions applied to any of the columns,right? so the CASE and CONVERT statements in the query you posted will disqualify the view from being indexed, but you can certainly index the underlying tables for performance.

Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1350428
Posted Monday, August 27, 2012 8:38 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:41 PM
Points: 13,315, Visits: 12,182
Luis Cazares (8/27/2012)
jdbrown239 (8/26/2012)
Need to create view with no outer joins so I can index the view.


I can't find a restriction for indexed views that involves outer joins http://msdn.microsoft.com/en-us/library/ms191432.aspx. Are you sure that's the reason? and that's what you need?
OUTER JOINS have a very specific function and can't be really replaced by something else without changing the results (OUTER APPLY is similar but I don't believe it'll solve your problem).


Indexed views must be deterministic. The link here will show you a number on things not allowed in an indexed view.

http://msdn.microsoft.com/en-us/library/ms191432%28v=sql.105%29.aspx


_______________________________________________________________

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 #1350430
Posted Monday, August 27, 2012 9:04 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 8:15 PM
Points: 3,564, Visits: 7,702
I guess it was wrong for me to read the 2012 version as it is less explicit.
But I still believe the OP won't have an option on creating the indexed view with this query. At least it won't give the same output.



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1350440
Posted Monday, August 27, 2012 9:07 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:41 PM
Points: 13,315, Visits: 12,182
Luis Cazares (8/27/2012)
I guess it was wrong for me to read the 2012 version as it is less explicit.
But I still believe the OP won't have an option on creating the indexed view with this query. At least it won't give the same output.


I would agree. I think that properly indexing the base tables as Lowell suggested is the most likely path to a solid solution in this case. With no more details to work with though it is hard to say.


_______________________________________________________________

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 #1350441
Posted Monday, August 27, 2012 12:00 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Saturday, August 30, 2014 5:24 PM
Points: 7,139, Visits: 15,191
Sean Lange (8/27/2012)
Luis Cazares (8/27/2012)
I guess it was wrong for me to read the 2012 version as it is less explicit.
But I still believe the OP won't have an option on creating the indexed view with this query. At least it won't give the same output.


I would agree. I think that properly indexing the base tables as Lowell suggested is the most likely path to a solid solution in this case. With no more details to work with though it is hard to say.


The XQUERY references (i.e. the stuff in the .value() constructs) will preclude this from being deterministic, so you won't be able to mark it schemabound. Without schema-bound you won't be able to index it.

It's actually one of the restrictions in the article mentioned above.


----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Post #1350511
Posted Monday, August 27, 2012 3:40 PM


SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Friday, August 8, 2014 11:44 AM
Points: 133, Visits: 282
Sorry for the initial messy post.

I think you guys are right. I have tried writing the view without joins and the result set is never the same. I will look at indexing the tables.

Thanks!
Post #1350624
Posted Tuesday, August 28, 2012 2:11 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:49 AM
Points: 7,234, Visits: 13,719
jdbrown239 (8/27/2012)
Sorry for the initial messy post.

I think you guys are right. I have tried writing the view without joins and the result set is never the same. I will look at indexing the tables.

Thanks!


Indexing won't help the WHERE clause, it isn't SARGable (the datetime functions prevent SQL Server from using any index on c.CTS_LAST_SAVED_DATE). If you remove those functions from c.CTS_LAST_SAVED_DATE, you may get a welcome performance boost:

WHERE c.CTS_LAST_SAVED_DATE > DATEADD(MINUTE,-50,GETDATE()) AND c.CTS_LAST_SAVED_DATE < DATEADD(MINUTE,-30,GETDATE()) 



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1350719
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse