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)
 Doing a join based on a condition

Author  Topic 

zajacv
Starting Member

2 Posts

Posted - 2008-04-28 : 09:45:51
Hello,
Is it possible based on values within certain columns, to either do a join or just pass a value? For example, If colA=1 and colB=2 and colC=3 then 1 as ColA else inner join on tableA.colA = tableb.ColA
Thanks

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-04-28 : 09:52:50
You have to elaborate more. Please post some sample data and expected output.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-28 : 10:34:37
You cant have condition based joins unless you use dynamic sql. But the same effect can be achieved by taking the join and using case in select list to selectively take the value. You need toprovide more information on what you are trying to achieve with some sample data from table and o/p you desire to get more help.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-28 : 10:40:29
You can, as a correlated subquery.
But remember that the correlated subquery only can return 1 column and 1 record.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

zajacv
Starting Member

2 Posts

Posted - 2008-04-28 : 11:27:09
Ok I have lookup table basically, lets call it tableA with colA and colB with the following data:
tableA
colA colB
123 1
321 2
544 3

then I have tableB with the following:
tableB
colA colB colC
123 1 c
321 2 b
555 3 c
421 4 c

If tableB colA = 555 and tableB colc = c then I want to pass 555 in colA however if these conditions are not met then I want to join to tableA on colA and return colB
I know I can do this doing a simple union all but its a fairly large table and I don't want to have to scan it twice by doing 2 separate selects
Thanks
Go to Top of Page
   

- Advertisement -