create table #temp1(patno int, hospno int, roomcode bit)
create table #temp2(patno int, hospno int, proc_code int)
create table #temp3(proc_code int, department int)
insert into #temp1
select 1, 123, null
union
select 1, 124,null
insert into #temp2
select 1, 123, 256
union
select 1, 123, 258
union
select 1, 123, 300
union
select 1, 124, 500
insert into #temp3
select 258, 701
union
select 200, 701
select * from #temp1
select * from #temp2
select * from #temp3
select *
from #temp1 t1
inner join #temp2 t2 on (t1.patno = t2.patno and t1.hospno = t2.hospno)
inner join #temp3 t3 on (t3.proc_code = t2.proc_code)
create table #temp2(patno int, hospno int, proc_code int)
create table #temp3(proc_code int, department int)
insert into #temp1
select 1, 123, null
union
select 1, 124,null
insert into #temp2
select 1, 123, 256
union
select 1, 123, 258
union
select 1, 123, 300
union
select 1, 124, 500
insert into #temp3
select 258, 701
union
select 200, 701
select * from #temp1
select * from #temp2
select * from #temp3
select *
from #temp1 t1
inner join #temp2 t2 on (t1.patno = t2.patno and t1.hospno = t2.hospno)
inner join #temp3 t3 on (t3.proc_code = t2.proc_code)