select t1.userid, t1.name, t1.createdate, t1.phonenumber, t1.status, t1.accountno, t2.usersiteid, t2.siteid, t2.usertype, t3.sitename, t4.departmentid, t4.hrid, t1.passwordchangedate, t1.isthirddeveloper from sys_users t1 inner join sys_usersite t2 on t1.userid = t2.userid inner join sys_site t3 on t2.siteid = t3.siteid left join sys_hr t4 on t1.userid = t4.userid and t2.siteid = t4.siteid inner join sys_site_parameter t6 on t3.siteid = t6.siteid and t6.loginmode_account = 1 where t1.accountno = $accountno$ and ifnull(t1.accountno, '')!='' and t1.status in ('ACTIVE', 'INACTIVE') and (('cweb' = $systemclient$ and t3.magsite = 1) or ('cweb'!=$systemclient$ and t3.magsite = 0)) order by case when t1.status = 'ACTIVE' then 1 when t1.status = 'INACTIVE' then 2 else 3 end