SELECT 
    OBJ_ADDRESS AS EVT_ADDRESS,
    OBJ_XCOORDINATE AS EVT_XCOORDINATE,
    OBJ_YCOORDINATE AS EVT_YCOORDINATE,
    OBJ_GISOBJID AS EVT_OBJECT_GISOBJID,
    EVT_CODE,
    EVT_DESC,
    EVT_JOBTYPE,
    ( SELECT UCO_DESC FROM r5ucodes
        WHERE uco_rentity = 'JBTP' 
              AND UCO_CODE = EVT_JOBTYPE
    ) AS EVT_JOBTYPE_DESC,
    EVT_MRC,
    EVT_OBJECT,
    EVT_OBJECT_ORG,
    EVT_OBTYPE,
    OBJ_DESC AS EVT_OBJECT_DESC,
    EVT_ORG,
    EVT_PPM,
    EVT_PPMREV,
    EVT_PRIORITY,
    EVT_RSTATUS,
    EVT_SAFETY,
    EVT_STATUS,
    ( SELECT UCO_DESC FROM r5ucodes
        WHERE uco_rentity = 'EVST'
        AND UCO_CODE = EVT_STATUS
    ) AS EVT_STATUS_DESC,
    EVT_WARRANTY,
    EVT_PERSON,
    EVT_LATITUDE,
    EVT_LONGITUDE,
    EVT_START,
    EVT_TARGET,
    EVT_REQUESTSTART,
    EVT_REQUESTEND,
    EVT_FROMPOINT  + COALESCE(OBJ_FROMPOINT,0) AS EVT_FROMPOINT,
    EVT_TOPOINT + COALESCE(OBJ_FROMPOINT,0) AS EVT_TOPOINT,
    PER_USER,
    ACT_ACT AS EVT_ACT_ACT,
    ACT_PERFORMEDBY AS EVT_ACT_PERFORMEDBY,
    ACT_PERFORMEDBY2 AS EVT_ACT_PERFORMEDBY2,
    ACT_REVIEWEDBY AS EVT_ACT_REVIEWEDBY,
    ACT_REJECTPERFORMEDBY AS EVT_ACT_REJECTPERFORMEDBY,
    ACT_REJECTPERFORMEDBY2 AS EVT_ACT_REJECTPERFORMEDBY2,
ACT_CONDITION_AS_FOUND AS EVT_ACT_CONDITION_AS_FOUND,
levelTb.LEVEL1 as LEVEL1,
levelTb.LEVEL2 as LEVEL2,
levelTb.LEVEL3A as LEVEL3A,
levelTb.LEVEL3 as LEVEL3,
levelTb.LEVEL4 as LEVEL4,
levelTb.equip,
levelTb.equiporg,
TSK_CODE,
(SELECT ins_desc  FROM r5install WHERE ins_code= 'CLRRWOST') AS TSK_CHECKLISTREJECTEDWOSTATUS,
(SELECT ins_desc FROM r5install WHERE ins_code= 'CLPBWOST') AS TSK_PERFORMEDBYWOSTATUS,
(SELECT ins_desc  FROM r5install WHERE ins_code= 'CLRBWOST') AS TSK_REVIEWEDBYWOSTATUS,
TSK_CLASS,
OBJ_CLASS as EVT_OBJ_CLASS,
OBJ_CLASS_ORG as OBJ_CLASS_ORG,
TSK_CHECKLISTPERFBYREQ,
TSK_CHECKLISTREVBYREQ,
ACT_PERFORMBYRESP,
ACT_PERFORMBY2RESP,
ACT_REVIEWRESP,
CASE (SELECT '+' FROM r5actchecklists WHERE ack_event=evt_code AND ack_act=act_act AND COALESCE(act_parent, act_act)=cast(act_act as real)) WHEN '+' THEN '+' ELSE '-' END as EVT_ACT_HASCHKLIST
FROM r5events e, r5activities a, r5objects
LEFT OUTER JOIN r5tasks ON act_task = tsk_code AND act_taskrev = tsk_revision
LEFT OUTER JOIN r5personnel ON PER_CODE=EVT_PERSON
LEFT OUTER JOIN (
SELECT lv1_code AS LEVEL1,null AS LEVEL2 ,null AS LEVEL3,null AS LEVEL3A,null AS LEVEL4, lv1_code AS equip, lv1_org AS equiporg FROM r5level1 WHERE lv1_code= equip AND lv1_org=equiporg
UNION
SELECT lv2_level1 AS LEVEL1,lv2_code AS LEVEL2 ,null AS LEVEL3,null AS LEVEL3A,null AS LEVEL4,lv2_code AS equip, lv2_org AS equiporg FROM r5level2 WHERE lv2_code=equip AND lv2_org= equiporg
UNION
SELECT lv3_level1 AS LEVEL1,lv3_level2 AS LEVEL2 ,lv3_code AS LEVEL3,null AS LEVEL3A,null AS LEVEL4, lv3_code AS equip, lv3_org AS equiporg FROM r5level3 WHERE lv3_code=equip AND lv3_org= equiporg
UNION
SELECT l3a_level1 AS LEVEL1,l3a_level2 AS LEVEL2 ,null AS LEVEL3,l3a_code AS LEVEL3A,null AS LEVEL4, l3a_code AS equip, l3a_org AS equiporg FROM r5level3a WHERE l3a_code=equip AND l3a_org= equiporg
UNION
SELECT lv4_level1 AS LEVEL1,lv4_level2 AS LEVEL2 ,lv4_level3 AS LEVEL3,null AS LEVEL3A,lv4_code AS LEVEL4, lv4_code AS equip, lv4_org AS equiporg FROM r5level4 WHERE lv4_code=equip AND lv4_org= equiporg
UNION
SELECT stc_parent LEVEL1, NULL LEVEL2, NULL LEVEL3, NULL LEVEL3A, NULL LEVEL4, stc_child equip, stc_child_org equiporg
FROM   r5level1, r5structures
WHERE  stc_parent     = lv1_code
AND    stc_parent_org = lv1_org
AND    lv1_rstatus IN ( 'B', 'C', 'CIR', 'CRR', 'I' )
AND    lv1_notused = '-'
UNION
SELECT lv2_level1, stc_parent, NULL, NULL, NULL, stc_child equip, stc_child_org
FROM   r5level2, r5structures
WHERE  stc_parent     = lv2_code
AND    stc_parent_org = lv2_org
AND    lv2_rstatus IN ( 'B', 'C', 'CIR', 'CRR', 'I' )
AND    lv2_notused = '-'
UNION
SELECT lv3_level1, lv3_level2, stc_parent, NULL, NULL, stc_child equip, stc_child_org
FROM   r5level3, r5structures
WHERE  stc_parent     = lv3_code
AND    stc_parent_org = lv3_org
AND    lv3_rstatus IN ( 'B', 'C', 'CIR', 'CRR', 'I' )
AND    lv3_notused = '-'
UNION
SELECT l3a_level1, l3a_level2, NULL, NULL, stc_parent, stc_child equip, stc_child_org
FROM   r5level3a, r5structures
WHERE  stc_parent     = l3a_code
AND    stc_parent_org = l3a_org
AND    l3a_rstatus IN ( 'B', 'C', 'CIR', 'CRR', 'I' )
AND    l3a_notused = '-'
UNION
SELECT lv4_level1, lv4_level2, lv4_level3, stc_parent, NULL, stc_child equip, stc_child_org
FROM   r5level4, r5structures
WHERE  stc_parent     = lv4_code
AND    stc_parent_org = lv4_org
AND    lv4_rstatus IN ( 'B', 'C', 'CIR', 'CRR', 'I' )
AND    lv4_notused = '-') as levelTb on obj_code = equip and obj_org = equiporg
WHERE evt_rstatus IN ( 'Q', 'R' ) 
       AND
       evt_rtype IN ( 'JOB', 'PPM' ) 
       AND
       EXISTS ( 
           SELECT 1
             FROM r5ucodes
            WHERE uco_rentity = 'JBTP' 
                  AND
                  uco_code = evt_jobtype 
                  AND
                  uco_rcode IN ( '*', 'BR', 'ST', 'PM', 'CAL', 'RP' )  
       )
       AND
        (  (<parameter.deptsec>  = 'OFF' ) 
           OR
           EXISTS ( 
               SELECT 1
                 FROM r5departmentsecurity
                WHERE dse_user = <parameter.r5user> 
                      AND
                      dse_mrc = evt_mrc 
           ) 
        )
AND act_event=evt_code
AND (
    ((tsk_activechecklist = '+' AND tsk_enableenhancedplanning <> '+') AND (SELECT 1 FROM r5actchecklists WHERE ack_event=evt_code AND ack_act=act_act AND COALESCE(act_parent, act_act)=cast(act_act as real)))
    OR
    (<parameter.incactwochklist> = '+' AND (ifnull(tsk_activechecklist, '-') = '-'))
    )
AND  evt_rstatus = 'R'
AND evt_object = obj_code
AND evt_object_org = obj_org
AND act_reviewedby is  NULL
ORDER BY CASE
   WHEN e.DMLTYPE='A' THEN 0
   ELSE 1
   END,
 <parameter.fieldname> <parameter.sortorder>
