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)
 Can we use case expression on the join!!!

Author  Topic 

Nitu
Yak Posting Veteran

81 Posts

Posted - 2006-03-20 : 17:21:39
Hi,

I need to write a query where i have four different tables - (tables 1 to 4) to join to another table - (table5).

Based on the value of a particular field (lets say field4) in table5 the corresponding records are in those different four tables.

The relation b/n the five tables is like this,

Table5-field1 (pk)
field2
field3
field4

Table1 has one to many records with table5, though not all records but only those who have field4 = 1
Table1-field1
field21
field31

The same goes with the rest of the tables.

So can i write a query like this,

select * from table5
inner join on table1 when table5.field4 = 1 or inner join on table2 when table5.field4 = 2 or inner join on table3 when table5.field4 = 3
or inner join on table4 when table5.field4 = 4

So any suggestions on how to do the above.

Thanks a lot in advance,
Nitu



Nitu
Yak Posting Veteran

81 Posts

Posted - 2006-03-20 : 17:48:48
quote:
Originally posted by Nitu

Hi,

I need to write a query where i have four different tables - (tables 1 to 4) to join to another table - (table5).

Based on the value of a particular field (lets say field4) in table5 the corresponding records are in those different four tables.

The relation b/n the five tables is like this,

Table5-field1 (pk)
field2
field3
field4

Table1 has one to many records with table5, though not all records but only those who have field4 = 1
Table1-field1
field21
field31

The same goes with the rest of the tables.

So can i write a query like this,

select * from table5
inner join on table1 when table5.field4 = 1 or inner join on table2 when table5.field4 = 2 or inner join on table3 when table5.field4 = 3
or inner join on table4 when table5.field4 = 4

So any suggestions on how to do the above.

Thanks a lot in advance,
Nitu







I just thought of a solution.

I am going to write four different queries and then do a Union of all.

If somebody can think of a better way, please reply.

thanks a lot in advance.
Nitu
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-03-20 : 18:53:52
The best way to do this is to redesign your database. You should not have tables designed this way.

Tara Kizer
aka tduggan
Go to Top of Page

Nitu
Yak Posting Veteran

81 Posts

Posted - 2006-03-21 : 10:50:28
I know, but hey i just work here, and i joined in the middle, when they have been using this for more than 2 years now. And my lead starts hating me the minute i talk about the design of the database. So what can i do, i believe thats part of the job.

Anyway, do u think there is any other way around for doing what i posted.

Thanks in advance,
Nitu
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-03-21 : 11:45:31
Given your (unfortunate) situation, your suggested solution seems like the best method to me.

Ryan Randall
www.monsoonmalabar.com

Ideas are easy. Choosing between them is the hard part.
Go to Top of Page
   

- Advertisement -