select e.*,epo.epo_frompoint + CAST( COALESCE( o.obj_frompoint, 0 ) AS NUMERIC( 24, 6 ) ) epo_frompoint ,epo.epo_topoint + CAST( COALESCE( o.obj_frompoint, 0 ) AS NUMERIC( 24, 6 ) ) epo_topoint
from r5objects o, r5eventpoints e LEFT OUTER JOIN r5points p ON (e.epo_object = p.poi_object AND e.epo_obtype = p.poi_obtype AND e.epo_object_org = p.poi_object_org AND e.epo_pointtype = p.poi_pointtype AND e.epo_point=p.poi_point),
(SELECT epo_code, epo_frompoint, epo_topoint
FROM   r5eventpoints
WHERE  epo_object = <param.object>
AND    epo_object_org =  <param.objorganization>
AND    epo_frompoint IS NOT NULL
AND    epo_topoint IS NOT NULL
AND    strftime('%Y-%m-%d', epo_date) <= strftime('%Y-%m-%d', <param.effectivedate>)
UNION ALL
SELECT epo_code, lrf_frompoint, lrf_topoint
FROM   r5eventpoints e1, r5objlinearref
WHERE  <param.includerelatedequip> = '+'
AND    lrf_objcode =  <param.object>
AND    lrf_objorg = <param.objorganization>
AND    lrf_displayonoverview = '+'
AND    epo_object = lrf_re_objcode
AND    epo_object_org = lrf_re_objorg
AND    strftime('%Y-%m-%d', epo_date) <= strftime('%Y-%m-%d', <param.effectivedate>)
AND NOT EXISTS( SELECT 'x'
FROM r5eventpoints e2
WHERE  epo_object = <param.object>
AND    epo_object_org =  <param.objorganization>
AND    epo_frompoint IS NOT NULL
AND    epo_topoint IS NOT NULL
AND    strftime('%Y-%m-%d', epo_date) <= strftime('%Y-%m-%d', <param.effectivedate>)
AND    e2.epo_code = e1.epo_code ) ) epo
where e.epo_code = epo.epo_code
and o.obj_code = e.epo_object
and o.obj_org = e.epo_object_org
