I'm working on a select query that selects the list of approvers for a given record based on the foreign key EFTRecIDNum. The list of approvers are stored in table tblApprover, with relevant fields EFTRecIDNum, Approver, ApprovalTime, and ApproverAction. The challenge is that I need all records for a given EFTRecIDNum expect where an approver has done the same action more then once (which does have a business purpose). In that case I would need to select the newest record. My data looks like this:
+------+-------------+----------+--------------+----------------+
| ID | EFTRecIDNum | Approver | ApprovalTime | ApproverAction |
+------+-------------+----------+--------------+----------------+
| 503 | 111 | Person A | 09-Jun-16 | Reviewed |
| 865 | 111 | Person B | 10-Jun-16 | Reviewed |
| 1517 | 111 | Person C | 11-Jun-16 | Reviewed |
| 1610 | 111 | Person C | 12-Jun-16 | Reviewed |
| 1743 | 111 | Person D | 13-Jun-16 | Approved |
| 1744 | 111 | Person C | 14-Jun-16 | Approved |
+------+-------------+----------+--------------+----------------+
Where I need to select everything but ID number 1517.
I've tried a few different things with joins and subselects but always end up only selecting the most recent reviewer/approver.
SELECT
a.EFTRecIDNum,
a.Approver,
a.ApprovalTime,
a.ApproverAction
FROM tblApprover a INNER JOIN
(SELECT ApproverAction, MAX(ApprovalTime) as sTime
FROM tblApprover GROUP BY ApproverAction) b
on a.ApproverAction=b.ApproverAction and a.ApprovalTime = b.sTime
Please let me know if there is anything I can add or clarify.
EDIT: Added another line of data to clarify.
1610, not1517. - Tim Biegeleisen(select EFTRecIDNum, Approver, ApproverAction max(approvalTime) sTime from tblApprover GROUP BY EFTRecIDNum, Approver, ApproverAction) b ON ...- xQbertI need all records for a given EFTRecIDNum expect where an approver has done the same action more then onceas that example isn't in sample data. - xQbert