I am trying to build reports for sport team. The results are stored in a SQL-Database like this:
For the reports I need the columns Home2-Away4 to be filled with the corresponding results. Which means:
Imagine: ID=10 and Home(no.) (Team#) = 1 --> Home1 = result one game before which is ID=3 and the team played away and the result was 0 (=home1). This is what I already have done, but Home2 = result two games ago, as Home3=result three games ago and so on.
And here is how I have done this:
SELECT top 1 [result_home] from (SELECT Top 2 [result_home] FROM
(SELECT top 2 [result_home] from [Table]
where [ID] < 10 and ( [Home(no.)]=1 or [Away(No.)]=1 ) order by [ID] desc ) top2 )top1
(Microsoft SQL Server Express 2012 btw.)
I have written a small Java code which decides if to pick from result_home or result_away.
The basic question is: How is it possible to query for game results two and more gamedays ago?
Thanks in advance.
Kind regards
Stefan