简体   繁体   中英

MySQL Return All Related Records where Count > x with Minimum Value

I'm completely new to MySQl, so forgive my lack of jargon to explain what I need to do. I'm trying to work out how to return all records where a related table has a minimum value of more than x number of times.

For example, I am tracking charitable donations for multiple charities. I want to pull a list of donors who have contributed to AT LEAST $25 to 3 DISTINCT organizations, one row for each qualifying transaction that includes * from all 3 tables.

I've tried using join with a subquery, but it's either the wrong approach or I'm just doing it wrong (v likely!).

I was trying a method I found here: SQL: Select rows with a column value that occurs at least N times? :

SELECT a.*, b.*
  FROM Transactions a
  JOIN (SELECT c.*
      FROM Contributor c
  GROUP BY 
    HAVING COUNT(*) >= 3 and Amount > 25) a.Amount ON b. = a.lname

Here's the diagram of the MySQL sb (using MS Access as front end):

在此处输入图片说明

What I came up with:

Query1 - aggregates by donor and charity and filters by Amount >=25, this query handles possibility of more than one 25+ donation by donor to same charity

SELECT Transactions.ContributorID, Transactions.OrganizationID
FROM Transactions
WHERE (((Transactions.Amount)>=25))
GROUP BY Transactions.ContributorID, Transactions.OrganizationID;

Query2 - aggregates Query1 by donor and counts charities

SELECT Query1.ContributorID, Count(Query1.OrganizationID) AS CountOfOrganizationID
FROM Query1
GROUP BY Query1.ContributorID
HAVING (((Count(Query1.OrganizationID))>=3));

Query3 - displays transactions where charity count >=3 and Amount >=25

SELECT Contributor.Company, Organization.CharityName, Transactions.Amount
FROM Query2 INNER JOIN (Organization INNER JOIN (Contributor INNER JOIN Transactions 
ON Contributor.ID = Transactions.ContributorID) 
ON Organization.ID = Transactions.OrganizationID) ON Query2.ContributorID = Transactions.ContributorID
WHERE (((Transactions.Amount)>=24) AND ((Query2.CountOfOrganizationID)>=3));

All in One:

SELECT Contributor.Company, Organization.CharityName, Transactions.Amount
FROM
(SELECT Query1.ContributorID, Count(Query1.OrganizationID) AS CountOfOrganizationID
FROM
(SELECT Transactions.ContributorID, Transactions.OrganizationID
FROM Transactions
WHERE (((Transactions.Amount)>=25))
GROUP BY Transactions.ContributorID, Transactions.OrganizationID) AS
Query1
GROUP BY Query1.ContributorID
HAVING (((Count(Query1.OrganizationID))>=3))) AS
Query2 INNER JOIN (Organization INNER JOIN (Contributor INNER JOIN Transactions ON Contributor.ID = Transactions.ContributorID) ON Organization.ID = Transactions.OrganizationID) ON Query2.ContributorID = Transactions.ContributorID
WHERE (((Transactions.Amount)>=24) AND ((Query2.CountOfOrganizationID)>=3));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM