Solved

T SQL Multiple Table Query

Posted on 2013-06-06
4
243 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 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 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

707 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