select distinct
e.EOB_OBJECT as equipment,
e.EOB_MRC as department,
e.EOB_TARGET as schedstartdate,
e.EOB_RSTATUS as statuscode,
e.EOB_JOBTYPE as workordertype,
e.EOB_EVENT as workordernum,
e.EOB_EVENT_DESC as workorderdesc,
eob.eob_frompoint + CAST( COALESCE( o.obj_frompoint, 0 ) AS NUMERIC( 24, 6 ) ) as frompoint,
eob.eob_topoint + CAST( COALESCE( o.obj_frompoint, 0 ) AS NUMERIC( 24, 6 ) ) as topoint,
COALESCE( u2.uco_iconpath, u2.uco_icon ) as priorityicon,
e.EOB_CLASS as woclass,
e.EOB_CLASS_ORG as woclassorganization,
e.EOB_PRIORITY as priority
FROM r5ucodes u,
(SELECT eob_event, eob_object, eob_object_org, eob_frompoint, eob_topoint
FROM   r5eventobjects
WHERE  eob_object = <param.object>
AND    eob_object_org = <param.objorganization>
AND    eob_frompoint IS NOT NULL
AND    eob_topoint IS NOT NULL
UNION ALL
SELECT eob_event, eob_object, eob_object_org, lrf_frompoint, lrf_topoint
FROM   r5eventobjects o1, r5objlinearref
WHERE  <param.includerelatedequip> = '+'
AND    eob_object = lrf_re_objcode
AND    eob_object_org = lrf_re_objorg
AND    lrf_objcode = <param.object>
AND    lrf_objorg = <param.objorganization>
AND    lrf_displayonoverview = '+'
AND NOT EXISTS( SELECT 'x'
FROM   r5eventobjects o2
WHERE  eob_object =<param.object>
AND    eob_object_org = <param.objorganization>
AND    eob_frompoint IS NOT NULL
AND    eob_topoint IS NOT NULL
AND    o2.eob_event = o1.eob_event ) ) eob,
r5eventobjects e
LEFT OUTER JOIN r5ucodes u2 ON (u2.uco_rentity = 'JBPR' AND u2.uco_code = eob_priority)
LEFT OUTER JOIN r5objects o ON (o.obj_code = e.eob_object AND o.obj_org = e.eob_object_org)
WHERE e.eob_event = eob.eob_event and e.eob_object=eob.eob_object
AND strftime('%Y-%m-%d', e.eob_reported) <= strftime('%Y-%m-%d', <param.effectivedate>)
AND e.eob_rstatus  in ( 'Q' , 'R','C')
AND u.uco_rentity  = 'JBTP'
AND u.uco_code = e.eob_jobtype
AND u.uco_rcode <> 'IS'
