SELECT
ptw_code AS ptw_code,
ptw_org AS ptw_org,
ptw_desc AS ptw_desc,
ptw_object AS ptw_object,
ptw_object_desc AS ptw_object_desc,
ptw_status AS ptw_status,
(
SELECT uco_desc FROM r5ucodes WHERE uco_rentity = 'PWST' AND uco_code = ptw_status
) AS status_desc,
ptw_type AS ptw_type,
(
SELECT uco_desc FROM r5ucodes WHERE uco_rentity = 'PWTP' AND uco_code = ptw_type
) AS type_desc,
ptw_class AS ptw_class,
ptw_daterequired AS ptw_daterequired

FROM r5permittowork
WHERE
(<parameter.deptsec> ='OFF' OR (EXISTS (SELECT 1 FROM r5departmentsecurity WHERE dse_user = <parameter.r5user> AND dse_mrc = ptw_mrc )))

