OsakaWebbie
Programmer
I have a main table that stores info on people and/or organizations (name, address, that sort of thing). Then I have some other tables for things like categories, events, etc. and then many-to-many relationship tables that link them. I also have a many-to-many table to link the main table to itself to show a relationship between a person and an organization he/she belongs to. Pretty standard stuff.
Users asked me to write a query to give a summary of information for a given "team" (a type of organization), including such logical things as the categories the team is in, the leader's name, the number of members, and the number of male and female members. I thought I knew how to do that - a big messy query with lots of joins, but doable. But I can't get the JOINs and GROUP BY to play together right, and data gets repeated. If I do just one of the things, it's okay, e.g.:
No problem - in my test case (a team in two categories and containing 9 members), I get the name and the two categories.
And this query:
dutifully produces the result of 9.
But if I combine those two:
I get 9 copies of each category name and 18 members.
When I put everything I want together...
...it correctly tells me things like the date range and leader's name, but anything in multiple goes wild - there are 1800 members on this team and they are all both male and female! ;-)
How do I separate these things so that I get the correct answers for each of the sets of joins instead of them compounding on each other? It occurred to me to try making views for the troublesome parts separately, but I haven't tried it (and if possible I'd rather figure out how to do it just in a query so that I can do other queries like it in the future without continually adding views to the database).
Users asked me to write a query to give a summary of information for a given "team" (a type of organization), including such logical things as the categories the team is in, the leader's name, the number of members, and the number of male and female members. I thought I knew how to do that - a big messy query with lots of joins, but doable. But I can't get the JOINs and GROUP BY to play together right, and data gets repeated. If I do just one of the things, it's okay, e.g.:
Code:
SELECT p.FullName TeamName, GROUP_CONCAT(cat.Category ORDER BY cat.Category SEPARATOR '\n') Categories
FROM person p
LEFT JOIN percat pc ON p.PersonID=pc.PersonID LEFT JOIN category cat ON pc.CategoryID=cat.CategoryID
WHERE p.PersonID=4787 GROUP by p.PersonID
And this query:
Code:
SELECT p.FullName TeamName, COUNT(poc.PersonID) Members
FROM person p
LEFT JOIN perorg poc ON p.PersonID=poc.OrgID LEFT JOIN person mc ON poc.PersonID=mc.PersonID
WHERE p.PersonID=4787 GROUP by p.PersonID
But if I combine those two:
Code:
SELECT p.FullName TeamName, GROUP_CONCAT(cat.Category ORDER BY cat.Category SEPARATOR '\n') Categories,
COUNT(poc.PersonID) Members
FROM person p
LEFT JOIN percat pc ON p.PersonID=pc.PersonID LEFT JOIN category cat ON pc.CategoryID=cat.CategoryID
LEFT JOIN perorg poc ON p.PersonID=poc.OrgID LEFT JOIN person mc ON poc.PersonID=mc.PersonID
WHERE p.PersonID=4787 GROUP by p.PersonID
When I put everything I want together...
Code:
SELECT p.FullName TeamName, GROUP_CONCAT(cat.Category ORDER BY cat.Category SEPARATOR '\n') Categories,
CONCAT('Available: ',MIN(attendance.AttendDate),' - ',MAX(attendance.AttendDate)) Dates,
ml.FullName Leader, COUNT(poc.PersonID) Members, COUNT(pom.PersonID) Male,
COUNT(pof.PersonID) Female
FROM person p
LEFT JOIN percat pc ON p.PersonID=pc.PersonID LEFT JOIN category cat ON pc.CategoryID=cat.CategoryID
LEFT JOIN attendance ON p.PersonID=attendance.PersonID
LEFT JOIN perorg pol ON p.PersonID=pol.OrgID LEFT JOIN person ml ON pol.PersonID=ml.PersonID
LEFT JOIN perorg poc ON p.PersonID=poc.OrgID LEFT JOIN person mc ON poc.PersonID=mc.PersonID
LEFT JOIN perorg pom ON p.PersonID=pom.OrgID LEFT JOIN person mm ON pom.PersonID=mm.PersonID
LEFT JOIN perorg pof ON p.PersonID=pof.OrgID LEFT JOIN person mf ON pof.PersonID=mf.PersonID
WHERE p.PersonID=4787 AND attendance.EventID=7 AND pol.Leader=1 AND mm.Sex='M' AND mf.Sex='F'
GROUP by p.PersonID
How do I separate these things so that I get the correct answers for each of the sets of joins instead of them compounding on each other? It occurred to me to try making views for the troublesome parts separately, but I haven't tried it (and if possible I'd rather figure out how to do it just in a query so that I can do other queries like it in the future without continually adding views to the database).