select 

    CASE WHEN STW_PRIORITY IS NULL THEN <param.priority> ELSE STW_PRIORITY END as priority,
    (Select uco_desc  from r5ucodes where uco_code=CASE WHEN STW_PRIORITY IS NULL THEN <param.priority> ELSE STW_PRIORITY END and uco_rentity='JBPR') as prioritydrption,
    CASE WHEN STW_REQM IS NULL THEN <param.problemcode> ELSE STW_REQM END as problemcode,
    s.stw_code as standardwo, 
    s.stw_desc as standardwodesc,
    STW_DURATION as stwoduration,
    CASE WHEN STW_WOCLASS IS NULL THEN <param.woclass> ELSE STW_WOCLASS END as woclass,
    CASE WHEN STW_WOCLASS IS NULL THEN <param.woclassorg> ELSE STW_WOCLASS_ORG END as woclassorganization,
    STW_JOBTYPE as workordertype,
    (select uco_desc from r5ucodes where uco_rentity = 'JBTP' and uco_rcode in ('*','BR', 'CAL','RP') and uco_code = STW_JOBTYPE) as workordertypedrption,
    CASE WHEN COALESCE(<param.description>,'')='' THEN s.stw_desc ELSE <param.description> END as stwodription
    FROM r5ucodes u, r5userorganization u1, r5standworks s  
    WHERE coalesce(s.stw_notused,'-')='-'
    AND s.stw_type in ('SWO','PSWO') 
    AND
    ( 
        ( s.stw_obclass IS NULL AND s.stw_object IS NULL )  
        OR
        ( 
            ( <param.equipclass> IS NOT NULL AND <param.category> IS NULL)
            AND
            (
                ( s.stw_obclass = <param.equipclass> AND s.stw_obclass_org = <param.equipclassorg>)
                AND s.stw_object IS NULL
            ) 
        )
        OR
        (
            ( <param.equipclass> IS NOT NULL AND <param.category> IS NOT NULL)
             AND
             (
                s.stw_obclass = <param.equipclass> 
                AND s.stw_obclass_org = <param.equipclassorg> 
                AND coalesce(s.stw_object, <param.category>) =<param.category> 
            ) 
        ) 
    )
    AND s.stw_org = u1.uog_org
    AND u1.uog_user    = <parameter.r5user>
    AND (u1.uog_common  = '+' OR u1.uog_org = <control.org>)  
    AND s.stw_jobtype = u.uco_code
    AND u.uco_rentity = 'JBTP'
    AND
    (
        (   <param.pagemode> = 'display' 
            AND u.uco_rcode IN ('*','ST','BR', 'PM', 'CAL','RP')
        )
        OR
        (
            <param.pagemode> = 'view' 
            AND
            (
                (   <param.rjobtype> IN ('*','BR') AND u.uco_rcode IN ('BR', '*'))
                OR
                (   <param.rjobtype> NOT IN ('*','BR') AND u.uco_rcode = <param.rjobtype>)
            )
        )
    )
    AND 
    (   exists 
        (   select 'x' from r5jobtypeauth j
            where j.jta_jobtype = s.stw_jobtype
            and (
                    (<param.pagemode> = 'display' AND j.jta_insert = '+')
                    OR
                    (<param.pagemode> = 'view' AND j.jta_update = '+')
                )
            and (   j.jta_group = <param.group> or j.jta_group= '*')
        )
        OR
        exists 
        ( select 'x'  from r5install I
            where i.ins_code = 'JTAUTH'
            and upper( i.ins_desc ) not in ( 'YES', '+' )
        )
    )