?
Solved

T SQL Multiple Table Query

Posted on 2013-06-06
4
Medium Priority
?
245 Views
Last Modified: 2013-06-10
Hi,  Just need a little help with an SQL query please.

I have a table 'StageExtensions' which has the following columns

ID
StageID
Name

The idea of this table is that it defines a number of Attributes for a Stage.  I also have an associated table EventStageDetailValue which has the following columns

ID
EventStageDetailsID
StageExtensionID
Value

The idea here is that the EventStageDetailValue table holds value for a specific EventStage and Stage Type.  My problem is that this table may or may not have values but I always want to be able to display the headings.  Essentially I want a query similar to

SELECT StageExtensions.Name, EventStageDetailValue.*
FROM StageExtensions
LEFT JOIN EventStageDetailValue ON EventStageDetailValue.StageExtensionID = StageExtensions.ID
WHERE StageID = 1 AND EventStageDetailValue.EventStageDetailID = 4

The problem with this query is that it does not return the headings from the StageExtensions table if there are no values in the SEventStageDetailValue table.

To be more specific, if the StageExtensions table contains entries for Name and Age I want the query to return rows for Name and Age as well as (any) Value from the EventStageDetailValue table.
0
Comment
Question by:ChrisMD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 1000 total points
ID: 39225737
The problem is your WHERE clause since you are referencing a column in EventStageDetalValue with EventStageDetailValue.EventStageDetailID = 4 you are negating the benefit of the LEFT JOIN.  Change that to:

WHERE StageID = 1
   AND (EventStageDetailValue.EventStageDetailID IS NULL
      OR EventStageDetailValue.EventStageDetailID = 4)
0
 
LVL 22

Assisted Solution

by:Thomasian
Thomasian earned 1000 total points
ID: 39225764
Or you could just move the condition to the ON clause

LEFT JOIN EventStageDetailValue ON EventStageDetailValue.StageExtensionID = StageExtensions.ID AND EventStageDetailValue.EventStageDetailID = 4
WHERE StageID = 1
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39228275
NO points pl.

Either of the above may be used, the point being that if using an outer join and then wanting filtering conditions on that table,  and to allow rows of the control table without a join, nulls must be catered for.

This may be implict (as Thomasian has used) because the outer joined table is not referenced in the where clause - or, it may be explicit (as BriCrowe has used) so that any condition included in the where clause on the outer joined table must also permit a NULL).

clear as mud? - not sure I've explained this too well.
0
 

Author Closing Comment

by:ChrisMD
ID: 39234783
Thanks Guys - I used the first solution and it worked perfectly but I think the second would also work so I have split the points - hope that is ok.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question