Inner Join based on column value in SQL Server 2008 -
i join 2 tables based on column value if pm.servicelevelid value 1 or null inner join on u.facilityid if pm.servicelevelid value 2 inner join on u.facilityserviceid these 2 tables pm, u have these columns: providermessages messageid, facilityserviceid, servicelevelid, facilityid, providertypeid user_fa facilityserviceid, userfacilityid, facilityid currently, have inner join select distinct messageid, userfacilityid, 9 #providermessages inner join #user_fa on (#user_fa.facilityid = #providermessages.facilityid or #providermessages.facilityid null) , (#user_fa.facilityserviceid = #providermessages.facilityserviceid) select distinct messageid, userfacilityid, 9 #providermessages join #user_fa on #providermessages.servicelevelid in (null,1,2) , ( ( #providermessages.servicelevelid in (null,1) , #user_fa.facilityid = #providermessages.facilityid) ...