SELECT NCO_PK AS nonconformitypk,
NCF_CODE AS nonconformity,
NCO_OBSERVATION AS observation,
NCF_DESC AS description,
NCF_ORG AS organization,
NCF_OBJECT AS equipment,
OBJ_DESC AS equipmentdesc,
OBJ_ORG as equipmentorg,
NCF_OBJECT_ORG AS NCF_OBJECT_ORG,
NCF_LOCATION AS NCF_LOCATION,
NCF_LOCATION_ORG AS NCF_LOCATION_ORG,
NCF_CLASS  AS NCF_CLASS,
NCF_CLASS_ORG AS NCF_CLASS_ORG,
NCO_TASK AS NCO_TASK,
NCO_TASKREV AS NCO_TASKREV,
NCF_SYSLEVEL AS NCF_SYSLEVEL,
NCF_ASMLEVEL AS NCF_ASMLEVEL,
NCF_COMPLEVEL AS NCF_COMPLEVEL,
NCF_PART AS NCF_PART,
NCF_PART_ORG AS NCF_PART_ORG,
NCF_MATLTYPE AS NCF_MATLTYPE,
NCF_SEVERITY AS header_severity,
(
SELECT UCO_DESC FROM r5ucodes WHERE uco_rentity = 'SEVE' AND UCO_CODE = NCF_SEVERITY
) AS header_severity_display,
NCO_STATUS AS status,
(
SELECT UCO_DESC FROM r5ucodes WHERE uco_rentity = 'NOST' AND UCO_CODE = NCO_STATUS
)
AS status_display,
NCF_STATUS AS headerstatus,
(
SELECT UCO_DESC FROM r5ucodes WHERE uco_rentity = 'NCST' AND UCO_CODE = NCF_STATUS
)
AS headerstatus_display,
(
SELECT UCO_RCODE FROM r5ucodes WHERE uco_rentity = 'NCST' AND UCO_CODE = NCF_STATUS
)
AS headerRstatus,
NCF_NCTTYPE AS type,
(
SELECT NCT_DESC FROM r5nonconformitytypes WHERE NCT_CODE = NCF_NCTTYPE
)
AS typedesc,
NCF_NCTTYPE_ORG AS NCF_NCTTYPE_ORG,
NCO_SEVERITY AS severity,
(
SELECT UCO_DESC FROM r5ucodes WHERE uco_rentity = 'SEVE' AND UCO_CODE = NCO_SEVERITY
) AS severity_display,
NCF_INTENSITY AS NCF_INTENSITY,
NCF_SIZE AS NCF_SIZE,
NCF_IMPORTANCE AS NCF_IMPORTANCE,
NCF_PRIORITY AS NCF_PRIORITY,
NCO_NOTE AS NCO_NOTE,
NCO_RECORDED AS NCO_RECORDED,
NCO_RECORDEDBY AS NCO_RECORDEDBY,
NCF_CONDITIONSCORE AS NCF_CONDITIONSCORE,
NCF_CONDITIONINDEX AS NCF_CONDITIONINDEX,
NCF_NEXTINSPECTDATE AS NCF_NEXTINSPECTDATE,
NCF_NEXTINSPECTDATEOVERRIDE AS NCF_NEXTINSPECTDATEOVERRIDE,
NCF_RECOMMREPAIRDATE AS NCF_RECOMMREPAIRDATE,
NCF_ESTLABORCOST AS NCF_ESTLABORCOST,
NCF_ESTMATLCOST AS NCF_ESTMATLCOST,
NCF_ESTMISCCOST AS NCF_ESTMISCCOST,
NCO_WORKORDER AS NCO_WORKORDER,
NCF_CREATED AS NCF_CREATED,
NCF_CREATEDBY AS NCF_CREATEDBY,
NCF_UPDATED AS NCF_UPDATED,
NCF_UPDATEDBY AS NCF_UPDATEDBY,
NCF_FROMPOINT AS NCF_FROMPOINT,
NCF_FROMREFDESC AS NCF_FROMREFDESC,
NCF_FROMGEOREF AS NCF_FROMGEOREF,
NCF_TOPOINT AS NCF_TOPOINT,
NCF_TOREFDESC AS NCF_TOREFDESC,
NCF_TOGEOREF AS NCF_TOGEOREF,
NCF_INSPECTIONDIRECTION AS NCF_INSPECTIONDIRECTION,
NCF_FLOW AS NCF_FLOW,
NCO_PERFORMED AS NCO_PERFORMED,
NCO_PERFORMEDBY AS NCO_PERFORMEDBY,
NCO_PERFORMEDTYPE AS NCO_PERFORMEDTYPE,
NCO_REVIEWED AS NCO_REVIEWED,
NCO_REVIEWEDBY AS NCO_REVIEWEDBY,
NCO_REVIEWEDTYPE AS NCO_REVIEWEDTYPE,
NCF_MRC as NCF_MRC,
NCF_NOTE AS NCF_NOTE,
NCO_INTENSITY AS NCO_INTENSITY,
NCO_SIZE AS NCO_SIZE,
NCO_IMPORTANCE AS NCO_IMPORTANCE,
NCO_CONDITIONSCORE AS NCO_CONDITIONSCORE,
NCO_CONDITIONINDEX AS NCO_CONDITIONINDEX,
NCO_CREATED AS NCO_CREATED,
NCO_CREATEDBY AS NCO_CREATEDBY,
NCO_UPDATED AS NCO_UPDATED,
NCO_UPDATEDBY AS NCO_UPDATEDBY,
NCO_NEXTINSPECTDATE AS NCO_NEXTINSPECTDATE,
NCO_NEXTINSPECTDATEOVERRIDE AS NCO_NEXTINSPECTDATEOVERRIDE,
(
SELECT NCP_USEOBSSTATUS FROM r5nonconformitysetup WHERE NCP_ORG = <param.organization>
) AS NCP_USEOBSSTATUS 
FROM r5organization, r5nonconformities
LEFT OUTER JOIN r5objects ON ( obj_code = ncf_object AND obj_org = ncf_object_org )
LEFT OUTER JOIN r5nonconformitytypes ON ( nct_code = ncf_ncttype )
,r5nonconformobservations LEFT OUTER JOIN r5events ON ( evt_code = nco_workorder )
WHERE org_code = ncf_org AND evt_code = <param.workordernum>
AND evt_org = <param.organization>
AND ncf_code = nco_code
AND (<parameter.deptsec> = 'OFF' OR
(EXISTS( SELECT dse_mrc FROM r5departmentsecurity
WHERE dse_user = <parameter.r5user> AND dse_mrc = obj_mrc)))
