I have a query which generates a soccer standings table and am trying to create a different version which will only include the 6 most recent games for each team.
An example of the output of my current query can be seen HERE. The full query is below and I have also attached a dump of the relevant tables in a text file.
I have asked the question many times on other forums and have googled 'Show Last N records per group' for months, including another 2 hour spell just now but everything I have tried just causes empty results.
To be honest, all of the suggestions I have looked at are for just one table so I think that with 3 tables being used in my query, the whole thing becomes more complex. It is, however, possible as most UK soccer sites have this (known as a 'Current Form' Table) and an example can be found at http://www.andysstats.co.uk/form.asp?division=P&type=LT&season=200910. Out of interest, the right-most column on that table shows the actual outcome of that team's last 6 games (W for won, L for Lost etc) and that is something else I would be looking at doing eventually.
Any suggestions or pointers would be gratefully received as always.
Thanks in advance
Steve
THE MAIN QUERY
$i = 1;
/*ADD th, st, rd TO THE POSITION NUMBERS*/
/*
* @return number with ordinal suffix
* @param int $number
* @param int $ss Turn super script on/off
* @return string
*/
function ordinalSuffix($i, $ss=0)
{
/*** check for 11, 12, 13 ***/
if ($i % 100 > 10 && $i %100 < 14)
{
$os = 'th';
}
/*** check if number is zero ***/
elseif($i == 0)
{
$os = '';
}
else
{
/*** get the last digit ***/
$last = substr($i, -1, 1);
switch($last)
{
case "1":
$os = 'st';
break;
case "2":
$os = 'nd';
break;
case "3":
$os = 'rd';
break;
default:
$os = 'th';
}
}
/*** add super script ***/
$os = $ss==0 ? $os : '<sup>'.$os.'</sup>';
/*** return ***/
return $i.$os;
};
/*GET THE CORRECT SEASON*/
if (isset($_GET['season_id']) ? $_GET['season_id'] : 1);
$season_id = $_GET['season_id'];
/*COMMON STANDINGS QUERY STRINGS*/
$ht = "g.home_team = t.team_id";
$at = "g.away_team = t.team_id";
$hw = "g.home_goals > g.away_goals";
$aw = "g.home_goals < g.away_goals";
$d = "g.home_goals = g.away_goals";
$hg ="g.home_goals";
$ag ="g.away_goals";
/*THE STANDINGS QUERY*/
$table = mysql_query("SELECT *,
t.team_name as Tm
, @rownum := @rownum+1 AS rank
, ((sum(CASE WHEN (".$ht." AND ".$hw.")OR(".$at." AND ".$aw.") THEN 3 ELSE 0 END)
+ sum(CASE WHEN (".$ht." OR ".$at.") AND ".$d." THEN 1 ELSE 0 END))) AS P
, count(CASE WHEN (".$ht." OR ".$at.") THEN 1 ELSE 0 END) as GP
, sum(CASE WHEN (".$ht." AND ".$hw.") OR (".$at." AND ".$aw.") THEN 1 ELSE 0 END) AS W
, sum(CASE WHEN (".$ht." AND ".$d.") OR (".$at." AND ".$d.") THEN 1 ELSE 0 END) AS D
, sum(CASE WHEN (".$ht." AND ".$aw.") OR (".$at." AND ".$hw.") THEN 1 ELSE 0 END) AS L
, SUM(CASE WHEN (".$ht.") THEN ".$hg." WHEN (".$at.") THEN ".$ag." END) as GF
, SUM(CASE WHEN (".$ht.") THEN ".$ag." WHEN (".$at.") THEN ".$hg." END) as GA
, (SUM(CASE WHEN (".$ht.") THEN ".$hg." WHEN (".$at.") THEN ".$ag." END)
- SUM(CASE WHEN (".$ht.") THEN ".$ag." WHEN (".$at.") THEN ".$hg." END)) as GD
, d.change as DIFF
from teams t
left join all_games g
on t.team_id in (g.home_team,g.away_team)
LEFT JOIN seasons as S ON g.date BETWEEN S.season_start AND S.season_end
LEFT JOIN deductions d ON ( d.team = t.team_id) AND (d.season = S.season_id)
WHERE comp = '1' AND home_goals IS NOT NULL
AND S.season_id = $season_id
GROUP BY t.team_id
ORDER BY P desc, GD desc, GF desc
");