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)
 SQL query required

Author  Topic 

vijmeena
Starting Member

10 Posts

Posted - 2003-01-03 : 00:49:27
I have 3 tables with the following data.

Table 1
Employee1
Employee2
Employee3
Employee4

Table 2
Employee2 details
Employee3 details

Table 3
Employee1 details
Employee4 details

How can I write a single query without using a union to get the details of all employees.

For e.g

Employee1 Employee1 details
Employee2 Employee2 details
Employee3 Employee3 details
Employee4 Employee4 details


mr_mist
Grunnio

1870 Posts

Posted - 2003-01-03 : 03:46:47
Erm. Well it depends how your tables are linked - I have to assume that there is some kind of relationship, like an employee ID... If your tables are exactly as you have shown in your example then I cannot do it, because there is no relationship.

Are the columns actually called "Employee2 details" or is that just an example of the data contained in the tables? Can you post the DDL for your actual tables?

If they are exactly as you have described you will, I guess, have to use some kind of dynamic SQL.

-------
Moo.
Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2003-01-03 : 06:56:49
What's wrong with a UNION? I mean, if you are not going to normalize your scheme, why worry about performance either?

Jay White
{0}
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-01-03 : 09:02:38
I assume you want all the data on one row for a unique Employee_Id...if so

Select * From
(Select * From Table1 Where Empl_Id = @EmplId) as a
, (Select * From Table2 Where Empl_Id = @EmplId) as b
, (Select * From Table3 Where Empl_Id = @EmplId) as c
Where a.Empl_Id = b.Empl_Id
And a.Empl_Id = c.Empl_Id

Of course I would reccomend not using *, but using the actual column names. You could also change it to be outer joins if Table1 is the driver and the other tables may or may not have data (that would be important). The predicate in the outer query is in case you want to eliminate the ones in the inner temp tables. It also depends on the relationships between the tables. If you have 1 to many relationships you will cartesian out...which might be ok for you...don't know with out more details.

Good Luck

Brett




Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-01-03 : 09:28:37
Assuming you have emp_id's in all these tables:

SELECT T1.*, T2.*, T3.*
FROM
Table1 T1
LEFT OUTER JOIN Table2 T2
ON T1.Emp_ID = T2.EmpID
LEFT OUTER JOIN Table3 T3
ON T1.Emp_ID = T3.EmpID

replace the * with the fields you need from each table.

is that what you mean?

- Jeff
Go to Top of Page

mr_mist
Grunnio

1870 Posts

Posted - 2003-01-03 : 09:40:13
I think it probably is, but it seems to me that you'd have to use at least one CASE statement somewhere along the line to produce output as shown in the question. Some of the employees are in table 2 but some are in table 3. Assuming there are emp_ids to join on and the tables are not literally as presented, then I'd suggest

SELECT t1.employee,
CASE when t2.employeedetail is not null
then t2.employeedetail
else t3.employeedetail end as EmployeeDetail
from table1 t1
left outer join table2 t2 on t1.emp_id = t2.emp_id
left outer join table3 t3 on t1.emp_id = t3.emp_id

Which should produce roughly what was in the question, assuming that an employee's details EITHER appear in table 2 or table 3 but not both, and that there is an emp_id to join on, and that the columns are not actually named as in the example.

There's too many assumptions for my liking, 's why I asked for proper DDL.

-------
Moo.

Edited because I can't count.

Edited by - mr_mist on 01/03/2003 09:41:24
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-01-03 : 10:01:30
You could also use ISNULL() for each field:

ISNULL(T2.Field1, T3.Field1) as Field1,
ISNULL(T2.Field2, T3.Field2) as Field2,
..etc..


- Jeff
Go to Top of Page
   

- Advertisement -