SELECT
NCF_CODE as nonconformity,
NCF_DESC as description,
NCF_ORG as organization,
NCF_OBJECT as equipment,
OBJ_DESC as equipmentdesc,
NCF_OBJECT_ORG as equipmentorg,
NCF_NCTTYPE AS type,
(
SELECT NCT_DESC FROM r5nonconformitytypes WHERE NCT_CODE = NCF_NCTTYPE
)
AS typedesc,
NCF_STATUS AS status,
(
SELECT UCO_DESC FROM r5ucodes WHERE uco_rentity = 'NCST' AND UCO_CODE = NCF_STATUS
)
AS headerstatus,
(
SELECT UCO_RCODE FROM r5ucodes WHERE uco_rentity = 'NCST' AND UCO_CODE = NCF_STATUS
)
AS rstatus,
NCF_SEVERITY AS severity,
(
SELECT UCO_DESC FROM r5ucodes WHERE uco_rentity = 'SEVE' AND UCO_CODE = NCF_SEVERITY
) AS headerseverity,
NCF_INTENSITY,
NCF_SIZE,
NCF_IMPORTANCE,
NCF_NEXTINSPECTDATEOVERRIDE,
NCF_NCTTYPE as type
FROM
  r5nonconformities n1,
  r5userorganization u,
  r5organization,
  r5objects o
WHERE
  org_code = ncf_object_org
  AND ncf_org = u.uog_org
  AND u.uog_org = <control.org>
  AND u.uog_user = <parameter.r5user>
  AND ncf_object = <param.equipment>
  AND ncf_object_org = <param.equipmentorg>
  AND o.obj_code = ncf_object
  AND o.obj_org = ncf_object_org
  AND    n1.ncf_rstatus  = 'O'
  AND NOT EXISTS( SELECT 'x'
                  FROM   r5nonconformobservations
                  WHERE  n1.ncf_code = nco_code
                  AND    nco_workorder = <param.workordernum>)


