I asked a similar question yesterday that was answered correctly. SQL requiring self join and ranking Although this question is similar, I'm unable to adapt the answer to fit.
I have a table of football results:
CREATE TABLE matches(
season NUMBER(4),
matchDate DATE,
homeTeam VARCHAR2(25),
awayTeam VARCHAR2(25),
homeGoals NUMBER(2),
awayGoals NUMBER(2),
totalGoals NUMBER(3));
INSERT statements can be found here: https://gist.github.com/2941229
I wish to calculate a value for totalGoals as follows. It is the total number of goals scored (homeGoals+awayGoals) in the 5 most recent matches played by the current home team added to the number of goals scored in the 5 most recent matches played by the current away team BEFORE the current match. It must NOT include the goals from the current match.
The difference with yesterday's question was that it only counted previous matches where the home team played at home and the away team played away. With this question, it doesn't matter if the home team previously played at home or away. They just had to be one of the teams in that previous match.
As before, only matches from the current season should be used, and if either team has not played 5 matches in the season, then totalGoals should remain NULL.
I can do this using PL/SQL, but I'd much prefer a SQL query. Yesterday's solution was about a thousand times faster than my PL/SQL.
Thanks Tams