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


creating a view to include fields which are NULL


creating a view to include fields which are NULL

Author
Message
mrichardson 57577
mrichardson 57577
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 82
Our webiste has several contact forms which all go into the same table.
We therefore have to create views to display the results from each webform (separating by fieldID and so on).

however, when a user leaves a field blank our fieldID does not record an entry and so the view fails to display the rest of the form.
part of the SQL code is below:

SELECT     TOP 100 PERCENT 
WebFormData_2.FieldValue AS Surname,
WebFormData_7.FieldValue AS SpecialReq
FROM db.R INNER JOIN
db.WebFormData WebFormData_2 ON db.R.WebFormRowId = WebFormData_2.WebFormRowId INNER JOIN
db.WebFormData WebFormData_7 ON db.R.WebFormRowId = WebFormData_7.WebFormRowId
WHERE
(WebFormData_2.WebFormFieldId =98) AND
(WebFormData_7.WebFormFieldId =99)



if we replace =99 with <>99
then this results in the form being displayed, but it also displays several times (with duplicate entries in the view).
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: 19016
Put in a specific check for NULL:

SELECT TOP 100 PERCENT 
WebFormData_2.FieldValue AS Surname,
WebFormData_7.FieldValue AS SpecialReq
FROM db.R
INNER JOIN db.WebFormData WebFormData_2
ON db.R.WebFormRowId = WebFormData_2.WebFormRowId
AND (WebFormData_2.WebFormFieldId = 98 OR WebFormData_2.WebFormFieldId IS NULL)
INNER JOIN db.WebFormData WebFormData_7
ON db.R.WebFormRowId = WebFormData_7.WebFormRowId
AND (WebFormData_7.WebFormFieldId = 99 OR WebFormData_7.WebFormFieldId IS NULL)



- with INNER JOINs, this can be in the ON or the WHERE clause.

“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
mrichardson 57577
mrichardson 57577
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 82
this is working now.
used:
SELECT TOP 100 PERCENT 
WebFormData_2.FieldValue AS Surname,
WebFormData_7.FieldValue AS SpecialReq
FROM db.R
JOIN db.WebFormData WebFormData_2
ON db.R.WebFormRowId = WebFormData_2.WebFormRowId
and WebFormData_2.WebFormFieldId =98
left JOIN db.WebFormData WebFormData_7
ON db.R.WebFormRowId = WebFormData_7.WebFormRowId
and WebFormData_7.WebFormFieldId =99


Phil Parkin
Phil Parkin
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8322 Visits: 19470
mrichardson 57577 (7/31/2012)
this is working now.
used:
SELECT TOP 100 PERCENT 
WebFormData_2.FieldValue AS Surname,
WebFormData_7.FieldValue AS SpecialReq
FROM db.R
JOIN db.WebFormData WebFormData_2
ON db.R.WebFormRowId = WebFormData_2.WebFormRowId
and WebFormData_2.WebFormFieldId =98
left JOIN db.WebFormData WebFormData_7
ON db.R.WebFormRowId = WebFormData_7.WebFormRowId
and WebFormData_7.WebFormFieldId =99



Just a quick adjunct - "TOP 100 PERCENT" is completely redundant in this query.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
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