Solved

T SQL Multiple Table Query

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

706 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

15 Experts available now in Live!

Get 1:1 Help Now