SELECT allitems.EVIDENCEDESCRIPTORID, allitems.SUCCESSIONID, allitems.EVIDENCETYPE, allitems.RELATEDID, allitems.PARTICIPANTID
INTO :evidenceDescriptorID, :successionID, :evidenceType, :evidenceID, :participantID
FROM
(SELECT
SUCCESSIONID, PARTICIPANTID, EVIDENCETYPE, MAX(effectiveFrom) AS maxeffectivefrom
FROM
(
SELECT
*
FROM
EVIDENCEDESCRIPTOR
WHERE effectiveFrom IS NOT NULL
AND caseID = :caseID
AND (STATUSCODE = 'EDS1' OR STATUSCODE = 'EDS2' OR STATUSCODE = 'EDS2001')
UNION
SELECT
*
FROM
EVIDENCEDESCRIPTOR
WHERE effectiveFrom IS NULL
AND caseID = :caseID
AND (STATUSCODE = 'EDS1' OR STATUSCODE = 'EDS2' OR STATUSCODE = 'EDS2001')
) all_maxitems
GROUP BY SUCCESSIONID, PARTICIPANTID, EVIDENCETYPE
) maxitems,
EVIDENCEDESCRIPTOR allitems
WHERE
allitems.successionID = maxitems.successionID
AND
(
(allitems.effectiveFrom IS NOT NULL AND (allitems.effectiveFrom=maxitems.maxeffectivefrom))
OR
(allitems.effectiveFrom IS NULL AND maxitems.maxeffectivefrom IS NULL)
)
AND
allitems.PARTICIPANTID = maxitems.PARTICIPANTID
AND
allitems.EVIDENCETYPE = maxitems.EVIDENCETYPE
AND
allitems.CASEID = :caseID
AND
(allitems.STATUSCODE = 'EDS1' OR allitems.STATUSCODE = 'EDS2' OR allitems.STATUSCODE = 'EDS2001')
|