select nct_nonconformity_class nonconformityclass,nct_nonconformity_class_org nonconformityclassorg,TSK_REVISION taskrevision,nct_severity severity,nct_chklst_task taskplanforchecklist,nct_code nonconformitytype,
nct_desc typedesc,nct_org typeorganization from  r5userorganization u, r5nonconformitytypes t
left outer join r5tasks on ( tsk_code = nct_chklst_task and tsk_org = nct_org and tsk_revrstatus = 'A' )
where  u.uog_user = <parameter.r5user> and  (u.uog_common ='+' or u.uog_org =  <control.org>)
and t.nct_org = u.uog_org
AND (COALESCE(nct_general, '-') = '+'
OR (EXISTS ( SELECT 'x' FROM   r5nonconformitytypeparts p
WHERE  p.ntp_nonconformitytype_code = t.nct_code
AND    p.ntp_nonconformitytype_org = t.nct_org
AND    p.ntp_part = COALESCE(<param.part>,'#')
AND    p.ntp_part_org = COALESCE(<param.part_org>,'#') ) )
OR (EXISTS( SELECT 'x' FROM   r5nonconformitytypemattype m
WHERE  m.mts_nonconformitytype = t.nct_code
AND    m.mts_nonconformitytype_org = t.nct_org
AND    m.mts_materialtype = COALESCE(<param.matltype> ,'#') ) )
OR (EXISTS( SELECT 'x' FROM   r5nonconformitytypeemrs e
WHERE  e.nte_nonconformitytype_code = t.nct_code
AND    e.nte_nonconformitytype_org = t.nct_org
AND    e.nte_syslevel = coalesce(<param.syslevel> , '#')
AND   ( nte_asmlevel = coalesce(<param.asslevel> , '#') or nte_asmlevel is null )
AND   ( nte_complevel = coalesce(<param.complevel> , '#') or nte_complevel is null ) ) ))
ORDER BY nct_code asc
