Solved

T SQL Multiple Table Query

Posted on 2013-06-06
4
241 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
4 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 250 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 250 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 48

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

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.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

697 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