SELECT z.closedate, z.znum, z.desknum FROM supermag.svcashz z WHERE z.locid = :MX AND ( EXISTS ( SELECT * FROM supermag.svdocnopricecs d, supermag.smdocprops p WHERE d.doctype IN ('CS', 'CR') AND NVL (d.locationfrom, d.locationto) = :MX AND z.closedate = d.createdat AND p.doctype = d.doctype AND p.docid = d.ID AND p.paramname = 'Cash.Invalid' AND p.paramvalue = '*') OR NOT EXISTS ( SELECT * FROM supermag.svdocnopricecs d, supermag.smdocprops p WHERE d.doctype IN ('CS', 'CR') AND NVL (d.locationfrom, d.locationto) = :MX AND z.closedate = d.createdat) ) ORDER BY z.closedate, z.znum, z.desknum