Given the following data set, how would I find the email addresses that were references for the most ApplicationID
s that have an "Accepted" decision?
CREATE TABLE IF NOT EXISTS `EmailReferences` (
`ApplicationID` INT NOT NULL,
`Email` VARCHAR(45) NOT NULL,
PRIMARY KEY (`ApplicationID`, `Email`)
);
INSERT INTO EmailReferences (ApplicationID, Email)
VALUES
(1, '[email protected]'), (1, '[email protected]'), (1, '[email protected]'),
(2, '[email protected]'), (2, '[email protected]'), (2, '[email protected]'),
(3, '[email protected]'), (3, '[email protected]'), (3, '[email protected]'),
(4, '[email protected]'), (4, '[email protected]'), (4, '[email protected]'),
(5, '[email protected]'), (5, '[email protected]'), (5, '[email protected]'),
(6, '[email protected]'), (6, '[email protected]'), (6, '[email protected]'),
(7, '[email protected]'), (7, '[email protected]'), (7, '[email protected]'),
(8, '[email protected]'), (8, '[email protected]'), (8, '[email protected]')
;
CREATE TABLE IF NOT EXISTS `FinalDecision` (
`ApplicationID` INT NOT NULL,
`Decision` ENUM('Accepted', 'Denied') NOT NULL,
PRIMARY KEY (`ApplicationID`)
);
INSERT INTO FinalDecision (ApplicationID, Decision)
VALUES
(1, 'Accepted'), (2, 'Denied'),
(3, 'Accepted'), (4, 'Denied'),
(5, 'Denied'), (6, 'Denied'),
(7, 'Denied'), (8, 'Accepted')
;
Fiddle of same:http://sqlfiddle.com/#!9/03bcf2/1
Initially, I was using LIMIT 1
and ORDER BY CountDecision DESC
, like so:
SELECT er.email, COUNT(fd.Decision) AS CountDecision
FROM EmailReferences AS er
JOIN FinalDecision AS fd ON er.ApplicationID = fd.ApplicationID
WHERE fd.Decision = 'Accepted'
GROUP BY er.email
ORDER BY CountDecision DESC
LIMIT 1
;
However, it occurred to me that I could have multiple email addresses that referred different "most accepted" decisions (i.e., a tie, so to speak), and those would be filtered out (is that the right phrasing?) with the LIMIT
keyword.
I then tried a variation on the above query, replacing the ORDER BY
and LIMIT
lines with:
HAVING MAX(CountDecision)
But I realized that that's only half a statement: MAX(CountDecision)
needs to be compared to something. I just don't know what.
Any pointers would be much appreciated. Thanks!
Note: this is for a homework assignment.
Update: To be clear, I'm trying to find value and count of Email
s from EmailReferences
. However, I only want rows that have FinalDecision.Decision = 'Accepted'
(on matching ApplicantID
s). Based on my data, the result should be:
Email | CountDecision
---------------+--------------
[email protected] | 2
[email protected] | 2