select 
    p.per_code as PERSONCODE,
         p.per_desc as description,
    CASE <param.booplan> WHEN 'true' THEN a.act_mrc ELSE p.per_mrc END as department,
    CASE <param.booplan> WHEN 'true' THEN a.act_trade ELSE p.per_trade END as emptrade


    from   r5userorganization u, r5personnel p, r5activities a where 
    p.per_notused = '-'      and  p.per_org = u.uog_org     
    and u.uog_user= <parameter.r5user>     
    and ( u.uog_common = '+' or u.uog_org = <control.org> )   
    and a.act_event = <param.event>     and a.act_act = <param.act>    
    and (<param.bypassdeptsecurity> = 'true' OR 
    (<parameter.deptsec> ='OFF' OR  (per_mrc IN( SELECT dse_mrc FROM 
    r5departmentsecurity WHERE dse_user =<parameter.r5user> AND 
    dse_mrc = per_mrc AND coalesce(dse_readonly, '-') = '-'))))
    ORDER BY p.per_code
