I’m a relative sql notice, eager to learn, but I need some help putting together a query. If anyone can steer me in the right direction, I’d be grateful.
The table is TeamRankings. The columns are as follows:
score | qualities_id | team_id | year | source
Teams are scored with respect to different qualities for specific years. Note that a given team can can be assigned multiple scores (each one would be from a different source). Likewise, a given team might not have any scores at all for given year.
Here’s the problem:
Given a single quality, and a list of teams, I want to find a list of years for which EACH of those teams has at least one score.
I’m coming at this from Rails (3.1), and I could do this a dumb way with Active Record and multiple db calls. But I’d like to be able to do in pure SQL. Bonus points for help in Active Record that limits to one db call.
Thanks much
Here’s an example for the case where you’d have 6 teams. The value you test in the HAVING clause (6 in my example), should match the number of team_ids included in the IN clause.