select
ack.*,
(
CASE
WHEN
(
SELECT
uco_rcode
FROM
r5ucodes
WHERE
uco_code        = ack_group_label
AND uco_rentity = 'GRPL'
)
= 'GBEQ'
THEN (ack_group_label ||
'#' ||
ack_object ||
'#' ||
ack_object_org)
ELSE ack_group_label
END
)
AS ack_group_key,
case when exists (select 1 from r5addetails where add_code = ack_taskchecklistcode and add_rentity = 'TCLI' AND add_lang=<parameter.language>) then '+' else '-' end as ack_comments,
(SELECT
ADD_TEXT
FROM
r5addetails
WHERE
add_code = ack_taskchecklistcode
AND add_rentity='TCLI' AND add_lang=<parameter.language>) AS ack_comment_text,
(
SELECT
CAST(COUNT(*) AS INT)
FROM
r5documents
WHERE
doc_entitycode=ack_code and doc_rentity='OPCL'
)
AS ack_doccount      ,
CASE WHEN EXISTS (SELECT 1 FROM r5ucodes WHERE uco_code = ack_group_label AND uco_rentity = 'GRPL' AND uco_rcode = 'GBEQ') THEN (ack_group_label + '#' + ack_object + '#' + ack_object_org) ELSE ack_group_label END as ack_group_key,
ack_group_label as ack_group_label,
(CASE WHEN EXISTS (SELECT 1 FROM r5ucodes WHERE uco_code = ack_group_label AND uco_rentity = 'GRPL' AND uco_rcode = 'GBEQ') THEN (ack_group_label + '#' + ack_object + '#' + ack_object_org) ELSE COALESCE(ack_group_label,'#') END) as ack_group_order,
obj_class as ack_object_class,
obj_desc as equipmentdescription,
obj_class_org as ack_object_classorg,
o.obj_gisobjid as ack_object_gisobjid,
coalesce( obj_frompoint, 0 ) + ack_frompoint as frompoint,
obj_linearrefuom as linearrefuom,
asp_desc                                     AS ack_aspect_desc     ,
poi_desc                                     AS ack_point_desc      ,
fnd_desc                                     AS ack_finding_desc    ,
coalesce( obj_frompoint, 0 ) + ack_topoint as topoint
FROM
r5actchecklists ack
LEFT OUTER JOIN
r5findings fin
ON
fnd_code= ack_finding
LEFT OUTER JOIN
r5aspects asp
ON
asp_code= ack_aspect
LEFT OUTER JOIN r5objects o ON obj_code = ack_object AND obj_org = ack_object_org
LEFT OUTER JOIN r5points poi ON poi_point = ack_point AND poi_pointtype = ack_pointtype AND poi_object = ack_object AND poi_object_org= ack_object_org
WHERE
ack_rentity                    = 'OPCK'
AND ack_entitykey                  = <param.operatorchecklistnumber>
AND ack_entityorg                  = <param.organization>
ORDER BY CASE ack_group_label
WHEN NULL
THEN 'zzzzzzz'
ELSE ack_group_label
END              ,
ack_routesequence,
ack_routeline    ,
ack_object       ,
ack_sequence     ,
ack_occurrence
