Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Create view with no outer joins


Create view with no outer joins

Author
Message
jdbrown239
jdbrown239
SSC-Enthusiastic
SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)

Group: General Forum Members
Points: 161 Visits: 390
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)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16550 Visits: 17004
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)
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8506 Visits: 18115
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.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14955 Visits: 38949
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16550 Visits: 17004
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)
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8506 Visits: 18115
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.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16550 Visits: 17004
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)
Matt Miller (#4)
Matt Miller (#4)
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7641 Visits: 18070
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?
jdbrown239
jdbrown239
SSC-Enthusiastic
SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)

Group: General Forum Members
Points: 161 Visits: 390
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!
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8959 Visits: 19017
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
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