SELECT
lv4_code AS code,
lv4_desc AS description,
lv4_org AS organization,
lv4_level1 AS level1,
lv4_level1_org AS level1org,
lv4_level2 AS level2,
lv4_level2_org AS level2org,
lv4_level3 AS level3,
lv4_level3_org AS level3org
FROM r5level4
WHERE lv4_rstatus IN ( 'B', 'C', 'CIR', 'CRR', 'I' )
AND lv4_notused = '-'
AND COALESCE( lv4_level1, 'x' ) = COALESCE( <param.level1>, lv4_level1, 'x' )
AND COALESCE( lv4_level1_org, 'x' ) = COALESCE( <param.level1_org>, lv4_level1_org, 'x' )
AND COALESCE( lv4_level2, 'x' ) = COALESCE( <param.level2>, lv4_level2, 'x' )
AND COALESCE( lv4_level2_org, 'x' ) = COALESCE( <param.level2_org>, lv4_level2_org, 'x' )
AND COALESCE( lv4_level3, 'x' ) = COALESCE( <param.level3>, lv4_level3, 'x' )
AND COALESCE( lv4_level3_org, 'x' ) = COALESCE( <param.level3_org>, lv4_level3_org, 'x' )
AND EXISTS ( SELECT 1 FROM r5userorganization WHERE uog_org = lv4_org AND uog_user = <parameter.r5user> AND ( uog_common  = '+' OR uog_org = <control.org> ) )
