SELECT LRF_REFDESC refdesc,
CAST (max(0, CAST ( (lrf_frompoint * uc.value) AS NUMERIC (24, 6) ) - CAST ([offset] AS NUMERIC (24, 6) ) ) AS NUMERIC (24, 6) ) + CAST( COALESCE( os.obj_frompoint, 0 ) AS NUMERIC( 24, 6 ) ) frompoint,
min(os.obj_length * (select value from R5UOMConversionMobile where fromuom = o.OBJ_LENGTHUOM AND touom = o.OBJ_LINEARREFUOM ), CAST ( (lrf_topoint * uc.value) AS NUMERIC (24, 6) ) - CAST ([offset] AS NUMERIC (24, 6) ) ) + CAST( COALESCE( os.obj_frompoint, 0 ) AS NUMERIC( 24, 6 ) ) topoint,
LRF_GEOREF georef,
os.lrf_id
FROM r5objlinearref l,
r5objects o,
R5UOMConversionMobile uc,/**this os temporary table will have all the offset for self and its ancestors linear reference whose apply to children is selected. os table runs two recursive query one for calculating offset and another for finding the ancestors.**/
(
SELECT lrf_id,
obj_length,
obj_frompoint,
obj_topoint,
obj_linearrefuom,
obj_lengthuom,
lrf_re_objcode,
lrf_re_objorg,
lrf_objcode,
lrf_objorg,
(/** ancestors offset recursive query finds the offset for all the ancestors which are relative to the current equipments length. It also send the 0 offset for self(ie when the parent and child is the same value as offset is not relevant in that case)**/
WITH RECURSIVE ancestorsOffset (
stc_parent,
stc_parent_org,
stc_child,
stc_child_org,
code,
org,
level,
frompoint
)
AS (
SELECT stc_parent,
stc_parent_org,
stc_child,
stc_child_org,
stc_child,
stc_child_org,
1 level,
l1.lrf_frompoint * uom.value
FROM r5objects o1,
r5objlinearref l1,
r5structures s1,
R5UOMConversionMobile uom
WHERE uom.fromuom = o1.obj_linearrefuom AND
uom.touom = a.obj_linearrefuom AND
l1.lrf_objcode = s1.stc_parent AND
l1.lrf_objorg = s1.stc_parent_org AND
l1.lrf_re_objcode = s1.stc_child AND
l1.lrf_re_objorg = s1.stc_child_org AND
o1.obj_code = l1.lrf_objcode AND
o1.obj_org = l1.lrf_objorg AND
s1.stc_child = a.obj_code AND
s1.stc_child_org = a.obj_org AND
(strftime('%s', 'now') BETWEEN strftime('%s', IfNULL(l1.lrf_dateeffective, DATETIME('now', '-1 days') ) ) AND strftime('%s', IfNULL(l1.lrf_dateexpired, DATETIME('now', '+1 days') ) ) OR
'+' = '-') AND
NOT                              EXISTS (
SELECT *
FROM r5objlinearref l3
WHERE l3.lrf_objcode = s1.stc_parent AND
l3.lrf_objorg = s1.stc_parent_org AND
l3.lrf_re_objcode = s1.stc_child AND
l3.lrf_re_objorg = s1.stc_child_org AND
strftime('%s', l3.lrf_dateeffective) > strftime('%s', l1.lrf_dateeffective) AND
strftime('%s', l3.lrf_dateeffective) <= strftime('%s', 'now')
)
AND
NOT                              EXISTS (
SELECT *
FROM r5objlinearref l4
WHERE l4.lrf_objcode = s1.stc_parent AND
l4.lrf_objorg = s1.stc_parent_org AND
l4.lrf_re_objcode = s1.stc_child AND
l4.lrf_re_objorg = s1.stc_child_org AND
strftime('%s', l4.lrf_dateeffective) < strftime('%s', l1.lrf_dateeffective) AND
strftime('%s', l1.lrf_dateeffective) > strftime('%s', 'now')
)
UNION ALL
SELECT s2.stc_parent,
s2.stc_parent_org,
s2.stc_child,
s2.stc_child_org,
ancestorsOffset.stc_child,
ancestorsOffset.stc_child_org,
ancestorsOffset.level + 1,
ancestorsOffset.frompoint + (l2.lrf_frompoint * uom1.value)
FROM R5UOMConversionMobile uom1,
r5objects o2,
r5objlinearref l2,
r5structures s2
JOIN
ancestorsOffset ON s2.stc_child = ancestorsOffset.stc_parent AND
s2.stc_child_org = ancestorsOffset.stc_parent_org
WHERE uom1.fromuom = o2.obj_linearrefuom AND
uom1.touom = a.obj_linearrefuom AND
l2.lrf_objcode = s2.stc_parent AND
l2.lrf_objorg = s2.stc_parent_org AND
l2.lrf_re_objcode = s2.stc_child AND
l2.lrf_re_objorg = s2.stc_child_org AND
o2.obj_code = l2.lrf_objcode AND
o2.obj_org = l2.lrf_objorg AND
(strftime('%s', 'now') BETWEEN strftime('%s', IfNULL(l2.lrf_dateeffective, DATETIME('now', '-1 days') ) ) AND strftime('%s', IfNULL(l2.lrf_dateexpired, DATETIME('now', '+1 days') ) ) OR
'+' = '-') AND
NOT                              EXISTS (
SELECT *
FROM r5objlinearref l5
WHERE l5.lrf_objcode = s2.stc_parent AND
l5.lrf_objorg = s2.stc_parent_org AND
l5.lrf_re_objcode = s2.stc_child AND
l5.lrf_re_objorg = s2.stc_child_org AND
strftime('%s', l5.lrf_dateeffective) > strftime('%s', l2.lrf_dateeffective) AND
strftime('%s', l5.lrf_dateeffective) <= strftime('%s', 'now')
)
AND
NOT                              EXISTS (
SELECT *
FROM r5objlinearref l6
WHERE l6.lrf_objcode = s2.stc_parent AND
l6.lrf_objorg = s2.stc_parent_org AND
l6.lrf_re_objcode = s2.stc_child AND
l6.lrf_re_objorg = s2.stc_child_org AND
strftime('%s', l6.lrf_dateeffective) < strftime('%s', l6.lrf_dateeffective) AND
strftime('%s', l6.lrf_dateeffective) > strftime('%s', 'now')
)
)
SELECT frompoint
FROM ancestorsOffset
WHERE code = a.obj_code AND
org = a.obj_org AND
stc_parent = b.LRF_OBJCODE AND
stc_parent_org = b.LRF_OBJORG
UNION ALL/**added the below code as a hack for returning 0 for self records as previosuly it was returning null for self records(ie when the child and parent is the same as offset value is not relevant for this case.)**/
SELECT 0
FROM dual
WHERE (
SELECT count( * )
FROM ancestorsOffset
WHERE code = a.obj_code AND
org = a.obj_org AND
stc_parent = b.LRF_OBJCODE AND
stc_parent_org = b.LRF_OBJORG
)
=                            0
)
[offset]
FROM r5objects a,
r5objlinearref b
WHERE a.obj_code = <param.obj_code> AND
a.obj_org = <param.obj_org> AND
b.lrf_objcode || '#' || b.lrf_objorg IN (
SELECT parent || '#' || parentorg
FROM r5objlinearref,
(/**this ancestors recursive query finds all the ancestors for the current leaf child which are still linear assets**/
WITH RECURSIVE ancestors (
stc_parent,
stc_parent_org,
stc_child,
stc_child_org,
level
)
AS (
SELECT stc_parent,
stc_parent_org,
stc_child,
stc_child_org,
1 AS level
FROM r5structures
WHERE stc_child = a.OBJ_CODE AND
stc_child_org = a.OBJ_ORG
UNION ALL
SELECT t.stc_parent,
t.stc_parent_org,
t.stc_child,
t.stc_child_org,
ancestors.level + 1
FROM ancestors
JOIN
r5structures t
WHERE ancestors.stc_parent = t.stc_child
)
SELECT stc_parent AS parent,
stc_parent_org AS parentorg,
stc_child AS child,
stc_child_org AS childorg,
level AS lev
FROM ancestors
)
WHERE lrf_objcode = parent AND
lrf_objorg = parentorg AND
lrf_re_objcode = child AND
lrf_re_objorg = childorg AND
strftime('%s', 'now') BETWEEN strftime('%s', IfNULL(lrf_dateeffective, DATETIME('now', '-1 days') ) ) AND strftime('%s', IfNULL(lrf_dateexpired, DATETIME('now', '+1 days') ) )
UNION
SELECT a.obj_code || '#' || a.obj_org
FROM dual
)
AND
(b.lrf_applytochildren = '+' OR
(b.lrf_objcode = a.obj_code AND
b.lrf_objorg = a.obj_org) )
)
os
WHERE l.lrf_id = os.lrf_id AND
o.obj_code = os.lrf_objcode AND
o.obj_org = os.lrf_objorg AND
uc.fromuom = o.OBJ_LINEARREFUOM AND
uc.touom = os.OBJ_LINEARREFUOM AND
(CASE WHEN <param.pointtype> = 'F' THEN lrf_frompoint ELSE lrf_topoint END * (select value from R5UOMConversionMobile where fromuom = o.OBJ_LINEARREFUOM AND touom = os.OBJ_LINEARREFUOM )) - [offset] BETWEEN 0 AND os.obj_length * (select value from R5UOMConversionMobile where fromuom = os.OBJ_LENGTHUOM AND touom = os.OBJ_LINEARREFUOM ) AND
(strftime('%s', IfNULL(lrf_dateeffective, DATETIME('now', '-1 days') ) ) <= strftime('%s', 'now') AND
strftime('%s', IfNULL(lrf_dateexpired, DATETIME('now', '-1 days') ) ) >= strftime('%s', 'now') ) ORDER BY CAST (max(0, CAST ( (lrf_frompoint * uc.value) AS NUMERIC (24, 6) ) - CAST ([offset] AS NUMERIC (24, 6) ) ) AS NUMERIC (24, 6) ) ASC,
min(os.obj_length * uc.value, CAST ( (lrf_topoint * uc.value) AS NUMERIC (24, 6) ) - CAST ([offset] AS NUMERIC (24, 6) ) ) ASC;


