MySQL Forums
Forum List  »  General

Help needed: Select With Count/Group By
Posted by: Dan Shapiro
Date: April 26, 2005 01:53PM

All,

I am an Oracle developer who is trying to get a handle on MySQL for a personal project and having trouble with queries that aren't supposed by MySQL that I could use in Oracle. Right now I am using MySQL 4.0, so I can't use any 4.1 or 5.0 features.

My data looks as follows
----------------------
| Team | TeamName |
----------------------
| 1 | a |
| 2 | b |
----------------------

----------------------------
| Team | Year | Ind1 | Ind2 |
----------------------------
| 1 | 2004 | Y | Y |
| 1 | 2005 | Y | N |
| 2 | 2004 | N | Y |
| 2 | 2005 | N | N |
----------------------------

I would like to somehow join these together to produce something that shows each Team and how many instances exist for a team with an indicator set to 'Y', aka:

------------------------------------
| Team | Count(Ind1) | Count(Ind2) |
------------------------------------
| 1 | 2 | 1 |
| 2 | 0 | 1 |
------------------------------------

So far I have been unable to write a query to do all of this at once. I have been able to get back the TeamID and the Count for any one indicator value, but when I try to include both Indicator counts, I am getting a Cartesian product and my counts are much higher than they should be.

Here's the actual query I am trying to run that is producing the cartesian product.

SELECT f.FranchiseID, f.franchiseName, count( sf.FranchiseID ) pas, count( sf2.FranchiseID ) series
FROM Franchise f
left outer join SeasonFranchise sf ON f.FranchiseID = sf.FranchiseID
and (
sf.WonDivision = 'Y'
or sf.WonWildcard = 'Y'
)
left outer join SeasonFranchise sf2 ON f.FranchiseID = sf2.FranchiseID AND sf.WonWorldSeries = 'Y'
GROUP BY f.FranchiseID, f.franchiseName
ORDER BY pas desc, series desc

Is this even possible in MySQL? I would probably just use an in-line view in Oracle SQL and this would be pretty easy, but I can't seem to find a way to do it in MySQL.

Thanks in advance for any assistance/advice. Also, if there is some way to do this with 4.1 or 5.0 that doesn't exist in 4.0, please let me know.

Thanks,
Dan

Options: ReplyQuote


Subject
Written By
Posted
Help needed: Select With Count/Group By
April 26, 2005 01:53PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.