select
ack_code as checklistcode,
ack_desc as checklistdescription,
ack_type as checklisttype,
ack_parentkey as parentkey,
tch_color as color,
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 comments,
ack_completed as completed,
ack_complevel as componentlevel,
ack_complocation as componentlocation,
ack_updated as dateupdated,
ack_defmaint as deferredmaintenanceno,
ack_entitykey as entitykeycode,
ack_object as equipment,
obj_class as equipmentclass,
obj_class_org as equipmentclassorg,
ack_object_org as equipmentorg,
ack_finaloccurrence as finaloccurrence,
ack_finding as finding,
ack_followup as followup,
CASE WHEN ack_followupevent IS NOT NULL AND cast(ack_followupact as nvarchar) IS NOT NULL THEN ack_followupevent + '-' + cast(ack_followupact as nvarchar) END as followupwoactivity,
coalesce( obj_frompoint, 0 ) + ack_frompoint as frompoint,
ack_fromgeoref as frompointgeoref,
ack_fromrefdesc as frompointrefdesc,
o.obj_gisobjid as gisobjid,
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 groupkey,
ack_fromjobplan as jobcode,
ack_fromjobplanrev as jobrevision,
obj_linearrefuom as linearrefuom,
NULL as messagecolumn,
ack_reading as meterreading,
ack_mobiledateupdated as mobiledateupdated,
ack_no as no,
ack_notes as notes,
ack_occurrence as occurrence,
COALESCE(ack_ok, '-') as ok,
COALESCE(ock_code,<param.operatorchecklistnumber>) as operatorchecklistnumber,
ack_entityorg as organization,
ack_point as point,
POI_DESC as pointdescription,
ack_possiblefindings as possiblefindings,
ack_updatecount as recordid,
tch_reference as reference,
case when tch_requiredtoclose = 'REG' then '+' else '-' end as regulated,
COALESCE(ack_repairsneeded, '-') as repairsneeded,
ack_requiredtoclose as requiredtoclose,
ack_resolution as resolution,
null as result,
ack_sequence as sequence,
ack_syslevel as systemlevel,
ack_taskchecklistcode as taskchecklistcode,
coalesce( obj_frompoint, 0 ) + ack_topoint as topoint,
ack_togeoref as topointgeoref,
ack_torefdesc as topointrefdesc,
ack_uom as uom,
ack_updatedby as updatedby,
ack_value as value,
ack_yes as yes
FROM
r5operatorchecklists, r5actchecklists a
LEFT OUTER JOIN r5taskplanjobs ON
ack_fromjobplan = tpj_jobplan AND
tpj_task = ack_task AND
TPJ_SEQ = CASE WHEN COALESCE(<param.jobplan>, 'SHOWALLRECORDS') = 'SHOWALLRECORDS' OR INSTR('#', <param.jobplan>) <= 0 THEN TPJ_SEQ
ELSE  substr(<param.jobplan>, 1, CASE WHEN INSTR('#', <param.jobplan>) > 0 THEN INSTR('#', <param.jobplan>)-1 ELSE 1 END)
END
LEFT OUTER JOIN r5taskchecklists ON ack_taskchecklistcode = tch_code
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 = COALESCE(ock_code,<param.operatorchecklistnumber>) AND
ack_entityorg = COALESCE(ock_org, <param.organization>)AND
ock_code = <param.operatorchecklistnumber> AND
COALESCE(ack_fromjobplan, '-') = CASE WHEN 'SHOWALLRECORDS' =
COALESCE(substr(<param.jobplan>, CASE WHEN INSTR('#', <param.jobplan>) > 0 THEN
INSTR('#', <param.jobplan>)+1 ELSE 1 END, length(<param.jobplan>) ), 'SHOWALLRECORDS')
THEN COALESCE(ack_fromjobplan, '-') ELSE substr(<param.jobplan>,
CASE WHEN INSTR('#', <param.jobplan>) > 0 THEN INSTR('#', <param.jobplan>)+1 ELSE 1 END, length(<param.jobplan>) ) END
ORDER BY ack_routesequence, ack_routeline, ack_object, ack_sequence, ack_occurrence
