Solved

T SQL Multiple Table Query

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now