SELECT TSK_REVISION taskrev, TSK_CODE task, TSK_DESC taskdesc, tsk_enableenhancedplanning enableenhancedplanning, TSK_REVIEWRESP as reviewresponsibility, TSK_PERFORMBYRESP as performbyresp, TSK_CHECKLISTREVBYREQ as reviewedbyrequired,TSK_CHECKLISTPERFBYREQ as performedbyrequired
FROM r5tasks t
LEFT OUTER JOIN r5fldfilteroprchecklist ON
ffo_task = t.tsk_code
AND ffo_function = 'WSOPCL'
AND ffo_obj_class = <param.equipclass>
AND ffo_obj_class_org = <param.equipclassorg>
WHERE t.tsk_revrstatus = 'A'
AND  COALESCE(t.tsk_notused, '-') <> '+'
AND COALESCE(t.tsk_disconnected_chklist,'-') = '+'
AND  COALESCE(t.tsk_isolationmethod, '-') = '-'
AND  COALESCE(t.tsk_casemanagementchecklist, '-') = '-'
AND  COALESCE(t.tsk_nonconformitychecklist, '-') = '-'
AND  COALESCE(t.tsk_jobplan, '-') = '-'
AND tsk_performby2resp IS NULL
AND (
t.tsk_performbyresp IS NULL OR
EXISTS (SELECT 1 FROM r5userresponsibilities WHERE urs_responsibility = t.tsk_performbyresp and urs_user = <parameter.r5user>)
)
AND t.tsk_code = CASE WHEN <param.equipmentcode> IS NOT NULL AND EXISTS (
SELECT 1 FROM r5fldfilteroprchecklist WHERE ffo_obj_class = <param.equipclass> AND ffo_obj_class_org = <param.equipclassorg> AND ffo_function = 'WSOPCL'
) THEN ffo_task ELSE t.tsk_code END
AND EXISTS (
SELECT 1 FROM r5taskchecklists WHERE tch_task = t.tsk_code AND tch_taskrev = t.tsk_revision  AND COALESCE(tch_notused, '-') = '-' AND COALESCE(t.tsk_activechecklist, '-') = '+'
UNION
SELECT 1 FROM r5tasks, r5taskchecklists, r5taskplanjobs WHERE tpj_task = t.tsk_code AND tsk_code = tpj_jobplan AND tsk_revision = tpj_taskrev AND tch_task = tpj_jobplan AND tch_taskrev = tpj_taskrev AND COALESCE(tch_notused, '-') = '-' AND COALESCE(tsk_jobplan, '-') = '+' AND COALESCE(tsk_activechecklist, '-') = '+')

