Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

SQL JOIN query help

 
   Database Help (Home) -> Programming RSS
Next:  Exporting Stored Procedure Results w/ SSIS?  
Author Message
daicamad

External


Since: Jun 26, 2008
Posts: 3



(Msg. 1) Posted: Tue Dec 02, 2008 12:43 pm
Post subject: SQL JOIN query help
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I need to do a query for a view. The query involves 3 tables, let call
them A, B, C. There is ItemID column in table A that match either
ItemID column on table B or C (but not both, because ItemIDs in table
B and C are assigned in a way that they cannot be the same)

How do I create a query that will JOIN these 3 tables together?

I start with this, but it doesn't work because I guess it expects
A.ItemID = B.ItemID = C.ItemID

SELECT A.a, A.b, B.Desc, C.Desc FROM [A] INNER JOIN [B] ON A.ItemID =
B.ItemID INNER JOIN [C] ON A.ItemID = C.ItemID;

 >> Stay informed about: SQL JOIN query help 
Back to top
Login to vote
ajames_2

External


Since: Dec 02, 2008
Posts: 2



(Msg. 2) Posted: Tue Dec 02, 2008 1:04 pm
Post subject: Re: SQL JOIN query help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Dec 2, 1:43 pm, daicamad wrote:
> I need to do a query for a view. The query involves 3 tables, let call
> them A, B, C. There is ItemID column in table A that match either
> ItemID column on table B or C (but not both, because ItemIDs in table
> B and C are assigned in a way that they cannot be the same)
>
> How do I create a query that will JOIN these 3 tables together?
>
> I start with this, but it doesn't work because I guess it expects
> A.ItemID = B.ItemID = C.ItemID
>
> SELECT A.a, A.b, B.Desc, C.Desc FROM [A] INNER JOIN [B] ON A.ItemID =
> B.ItemID INNER JOIN [C] ON A.ItemID = C.ItemID;

Try this:

SELECT
A.a,
A.b,
COALESCE(B.Desc, C.Desc) Desc
FROM A LEFT JOIN B ON A.ItemID = B.ItemID
LEFT JOIN C ON A.ItemID = C.ItemID;

 >> Stay informed about: SQL JOIN query help 
Back to top
Login to vote
daicamad

External


Since: Jun 26, 2008
Posts: 3



(Msg. 3) Posted: Tue Dec 02, 2008 2:27 pm
Post subject: Re: SQL JOIN query help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Dec 2, 4:04 pm, wrote:
> On Dec 2, 1:43 pm, daicamad wrote:
>
> > I need to do a query for a view. The query involves 3 tables, let call
> > them A, B, C. There is ItemID column in table A that match either
> > ItemID column on table B or C (but not both, because ItemIDs in table
> > B and C are assigned in a way that they cannot be the same)
>
> > How do I create a query that will JOIN these 3 tables together?
>
> > I start with this, but it doesn't work because I guess it expects
> > A.ItemID = B.ItemID = C.ItemID
>
> > SELECT A.a, A.b, B.Desc, C.Desc FROM [A] INNER JOIN [B] ON A.ItemID =
> > B.ItemID INNER JOIN [C] ON A.ItemID = C.ItemID;
>
> Try this:
>
> SELECT
>   A.a,
>   A.b,
>   COALESCE(B.Desc, C.Desc) Desc
> FROM A LEFT JOIN B ON A.ItemID = B.ItemID
> LEFT JOIN C ON A.ItemID = C.ItemID;

Cool! It works. I didnot know about COALESCE function.

Thanks
 >> Stay informed about: SQL JOIN query help 
Back to top
Login to vote
Rick Sawtell

External


Since: Jan 14, 2008
Posts: 104



(Msg. 4) Posted: Tue Dec 02, 2008 3:05 pm
Post subject: Re: SQL JOIN query help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"daicamad" wrote in message

>I need to do a query for a view. The query involves 3 tables, let call
> them A, B, C. There is ItemID column in table A that match either
> ItemID column on table B or C (but not both, because ItemIDs in table
> B and C are assigned in a way that they cannot be the same)
>
> How do I create a query that will JOIN these 3 tables together?
>
> I start with this, but it doesn't work because I guess it expects
> A.ItemID = B.ItemID = C.ItemID
>
> SELECT A.a, A.b, B.Desc, C.Desc FROM [A] INNER JOIN [B] ON A.ItemID =
> B.ItemID INNER JOIN [C] ON A.ItemID = C.ItemID;

SELECT <column list>
FROM A
JOIN B
ON A.ItemID = B.ItemID

UNION

SELECT <column list>
FROM A
JOIN C
ON A.ItemID = C.ItemID




Rick Sawtell
 >> Stay informed about: SQL JOIN query help 
Back to top
Login to vote
mouser

External


Since: Sep 30, 2008
Posts: 44



(Msg. 5) Posted: Tue Dec 02, 2008 5:26 pm
Post subject: Re: SQL JOIN query help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 2-Dec-2008, daicamad wrote:

> I need to do a query for a view. The query involves 3 tables, let call
> them A, B, C. There is ItemID column in table A that match either
> ItemID column on table B or C (but not both, because ItemIDs in table
> B and C are assigned in a way that they cannot be the same)
>
> How do I create a query that will JOIN these 3 tables together?
>
> I start with this, but it doesn't work because I guess it expects
> A.ItemID = B.ItemID = C.ItemID
>
> SELECT A.a, A.b, B.Desc, C.Desc FROM [A] INNER JOIN [B] ON A.ItemID =
> B.ItemID INNER JOIN [C] ON A.ItemID = C.ItemID;

It's a little unclear precisely what you are wanting to return and I have a
strong feeling your table design in not optimal. Nevertheless, this might
be a start if I am understanding you correctly:

SELECT A.a,A.b,COALESCE(B.Desc,C.Desc)
FROM A
LEFT JOIN B ON A.ItemID = B.ItemID
LEFT JOIN C ON A.ItemID = C.ItemID
WHERE NOT(B.ItemID IS NULL
AND C.ItemID IS NULL)

The WHERE clause prevents rows from being returned that are in A but are not
in B or C (not sure if you wanted this or not).

mouser
 >> Stay informed about: SQL JOIN query help 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
left join/inner join/outer join with repeat values - hi david, thanks for the reply. basically what i need this for is: there are two tables: table1 is a master table, table2 is a detail table. table1's primary_id is present in details_id of table2 table1 has only 1 record table2 could have many..

JOIN query - Hi, I have join question.. let's say I have two tables Create table #Tbltemp1 (AcntId varchar (20), AddB int) Create table #Tbltemp2 (AcntId varchar (20), RemoveB int) insert into #Tbltemp1 values ('RT1', '10') insert into #Tbltemp1 values ('RT2',..

many to many join query - Hi I have the following structure CaseStudy ---> CaseStudyClassification <-- ClassificationItem I have written the following query but I know that their must be a more elegant solution but I cant really figure it out. Can anyone help me? Thank...

Need help with Indexing this join query - select p.addressline1, p.addressline2, p.addressline3, p.municipality, c.candidateid from candidates c left join postaladdress p ON p.EntityID = c.CandidateID where entitytypeid = 1 I have an index on postaladdress.entityid and candidates.candidateid. ...

insert query with join - Hi Friends I am using SQL server 2005 as a database in my project. Situation is like this: I have two tables namely Applicant and PersonalDetails. First we are inserting values in Applicant Table and after that in PersonalDetails. In Applicant Table,..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]