Based on codittion retrive data in sql server

Same query as Check condition in sql server, just add this line to select section:

      ,isnull(a2.deptname,a1.deptname) as deptname