conditional joinson columns

Hello 

I have a question...

i have 4 tables 

location table (locationid ,address1 address2)

exception table( idcol1,locationid,createdby)

table1   (locationid,idcol1)

look up table -- idcol1 firstname lastname

exception table will be populated only for exceptions for that location

so this case i need to join between location table exception table and look up table 

otherwise i need to join 

location table ,table1 and look up table

 i have other tables as well to join before these table to get to these tables

location table left join table 1 on location id 

left join exception table on locationid 

left join on look up table on isnull(idcol1,table1.idcol1) = lokup table.idcol1

does this sound right?

August 31st, 2015 8:45pm

Hi madhavi,

Your requirement is not clear, base on my understanding, it seems that you're attempt to join tables based on the condition as below.

IF EXISTS(SELECT 1 FROM location l INNER JOIN exception e ON l.locationId = e.locationId)
	SELECT * FROM location l LEFT JOIN exception e
									ON l.locationId=e.locationId
							 LEFT JOIN look_up_table lt
									ON e.idcol1 = lt. e.idcol1

ELSE
	SELECT * FROM location l LEFT JOIN tabl1 t
									ON l.locationId=t.locationId
							 LEFT JOIN look_up_table lt
									ON t.idcol1 = lt. e.idcol1

If above is not what you're after, can you be more specific?

For any question, feel free to let me know.
Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 9:55pm

It would be a lot easier to help you if you were to post create table statements, some test data (in the form of insert statements), and the desired output based on that test data.
August 31st, 2015 10:23pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics