Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Complex SQL Query

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-04-02 : 08:53:00
Predator writes "Hi all,

I am developing an application in Visual Basic 6 using SQL Server as Back-end. I am facing a problem in creating a SQL Query. The details are as follows:

There are three tables in the Database, Data Type of all Columns is Numeric in all three tables:

1. T1

(column names and sample data)

en
==

1
2
3


2) T2


(column names and sample data)

en gn
== ==

1 10
1 11
2 10
2 12
2 13


3) T3

(column names and sample data)

en pn
== ==

1 20
1 21
1 22
2 20


Now I have to create a SQL Query, whereby I can get the following result:


en gn pn
== == ==
1 10 20
1 11 21
1 NULL 22
2 10 20
2 12 NULL
2 13 NULL


I have tried various combination of Joins, but unable to get the desired result as the tables have many-to-many relationships, therefore I get many duplicate rows in the result. UNION will not solve the problem, as that will add the additional rows for the third table. Although I can achieve this by writing few lines of code, but I have to create a SQL Query for getting this result. Kindly tell me the way for creating the required Query for this. Many Thanks for your help."

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-04-02 : 09:05:43
select t1.en, t2.gn, t3.gn
from t1
left outer join t2
on t1.en = t2.en
left outer join t3
on t1.en = t3.en


Duane.
Go to Top of Page
   

- Advertisement -