I’m trying to write a sql query that shows how often two teams have played against each other.
Id | Team1 | Team2 | Date
1 | A | B | 25/5/11
2 | B | A | 26/5/11
3 | A | C | 27/5/11
4 | C | B | 28/5/11
5 | A | B | 28/5/11
result should be:
A vs B => 3
A vs C => 1
C vs B => 1
Counting A-B and B-A as different is an easy query. But I can’t get them to be counted together.
Any suggestions?
Edit: Updated to reverse teams when necessary.
Note: This will run a lot faster than the versions using CASE you have been given in other answers because it will make full use of indexes.
Edit2: Moved the where to be even faster with indexes.