SELECT
l3a_code AS code,
l3a_desc AS description,
l3a_org AS organization,
l3a_level1 AS level1,
l3a_level1_org AS level1org,
l3a_level2 AS level2,
l3a_level2_org AS level2org
FROM r5level3a
WHERE l3a_rstatus IN ( 'B', 'C', 'CIR', 'CRR', 'I' )
AND l3a_notused = '-'
AND COALESCE( l3a_level1, 'x' ) = COALESCE( <param.level1>, l3a_level1, 'x' )
AND COALESCE( l3a_level1_org, 'x' ) = COALESCE( <param.level1_org>, l3a_level1_org, 'x' )
AND COALESCE( l3a_level2, 'x' ) = COALESCE( <param.level2>, l3a_level2, 'x' )
AND COALESCE( l3a_level2_org, 'x' ) = COALESCE( <param.level2_org>, l3a_level2_org, 'x' )
AND EXISTS ( SELECT 1 FROM r5userorganization WHERE uog_org = l3a_org AND uog_user = <parameter.r5user> AND ( uog_common  = '+' OR uog_org = <control.org> ) )
