SELECT DISTINCT
ast_severity ,
ast_createnonconformity ,
ast_nonconformitytype   ,
ast_nonconformitytype_org,
ack_code
FROM
r5aspecttolerance ,
r5objects,
r5actchecklists
WHERE
obj_code                       = ack_object
AND obj_org                    = ack_object_org
AND ack_code                   = <parameter.ackcode>
AND COALESCE(ast_aspect,'X')   =COALESCE(<parameter.aspect>,'X')
AND COALESCE(ast_pointtype,'X')=COALESCE(<parameter.pointtype>,'X')
AND ast_org                    = <parameter.astorg>
AND ast_uom                    = <parameter.astuom>
AND ast_measurement            ='+'
AND
(
CASE
WHEN ast_class   IS NOT NULL
THEN ast_class          =obj_class
AND ast_class_org       = obj_class_org
ELSE ast_class    IS NULL
and ast_class_org IS NULL
and ast_object    IS NULL
end
)
AND
(
CASE
WHEN ast_object IS NOT NULL
THEN ast_object        =obj_category
ELSE ast_object  IS NULL
end
)
AND ast_fromvalue <= <parameter.fromvalue>
AND ast_tovalue   >= <parameter.tovalue>
AND
(
CASE
WHEN obj_tolerance_size IS NULL
THEN (ast_from_objectsize IS NULL
AND ast_to_objectsize     IS NULL)
ELSE (obj_tolerance_size BETWEEN COALESCE(ast_from_objectsize,-9999999) AND COALESCE(ast_to_objectsize,9999999))
END
)


