sionainn
10-16-2006, 01:49 AM
Hello, I am working on a web site for statistics and am hoping someone can help me with several issues:
1) I want to find the current streak for each time. For example, Team A has won the last 5 games, so their streak would be '5 wins', and Team B lost their last games, but won the game before, so their streak would be '1 loss'
2) I know how to calculate the ranking order/position for the teams in a season, but is there a way to make a temporary column with the rank in it so the table will be sortable by visitors, without having to use php to input the rank number?
3) How do I write the following query better? I am sure this will bring cringes, but I am fairly new to more advanced queries beyond simple selects, so please don't be too harsh. :)
SELECT
team_id AS id,
team_name AS name,
(SELECT COUNT(game_id) FROM nfl_games WHERE ((game_homeTeam = id && game_homeScore > game_awayScore) || (game_awayTeam = id && game_awayScore > game_homeScore)) && game_dateTime > 1136073600 && game_dateTime < 1167609599) AS wins,
(SELECT COUNT(game_id) FROM nfl_games WHERE (game_homeTeam = id || game_awayTeam = id) && (game_homeScore > 0 || game_awayScore > 0) && game_awayScore=game_homeScore && game_dateTime > 1136073600 && game_dateTime < 1167609599) AS ties,
(SELECT COUNT(game_id) FROM nfl_games WHERE (((game_homeTeam = id) && game_homeScore < game_awayScore) || ((game_awayTeam = id) && (game_awayScore < game_homeScore))) && (game_dateTime > 1136073600) && (game_dateTime < 1167609599)) AS losses,
(SELECT wins + ties + losses) as total_games,
(SELECT SUM(game_homeScore) FROM nfl_games WHERE game_homeTeam = id && game_dateTime > 1136073600 && game_dateTime < 1167609599) AS home_points,
(SELECT SUM(game_awayScore) FROM nfl_games WHERE game_awayTeam = id && game_dateTime > 1136073600 && game_dateTime < 1167609599) AS away_points,
(SELECT IFNULL(home_points,0) + IFNULL(away_points,0)) AS points_for,
(SELECT FORMAT((points_for/total_games),3)) as game_average,
(SELECT FORMAT((wins/total_games),3)) as win_percentage,
(SELECT SUM(game_awayScore) FROM nfl_games WHERE (game_homeTeam = id) && game_dateTime > 1136073600 && game_dateTime < 1167609599) AS opp_home_points,
(SELECT SUM(game_homeScore) FROM nfl_games WHERE game_awayTeam = id && game_dateTime > 1136073600 && game_dateTime < 1167609599) AS opp_away_points,
(SELECT IFNULL(opp_home_points,0) + IFNULL(opp_away_points,0)) AS points_against
FROM nfl_teams
ORDER BY wins DESC, ties DESC, losses ASC
Thank you very much for any help you can give!
1) I want to find the current streak for each time. For example, Team A has won the last 5 games, so their streak would be '5 wins', and Team B lost their last games, but won the game before, so their streak would be '1 loss'
2) I know how to calculate the ranking order/position for the teams in a season, but is there a way to make a temporary column with the rank in it so the table will be sortable by visitors, without having to use php to input the rank number?
3) How do I write the following query better? I am sure this will bring cringes, but I am fairly new to more advanced queries beyond simple selects, so please don't be too harsh. :)
SELECT
team_id AS id,
team_name AS name,
(SELECT COUNT(game_id) FROM nfl_games WHERE ((game_homeTeam = id && game_homeScore > game_awayScore) || (game_awayTeam = id && game_awayScore > game_homeScore)) && game_dateTime > 1136073600 && game_dateTime < 1167609599) AS wins,
(SELECT COUNT(game_id) FROM nfl_games WHERE (game_homeTeam = id || game_awayTeam = id) && (game_homeScore > 0 || game_awayScore > 0) && game_awayScore=game_homeScore && game_dateTime > 1136073600 && game_dateTime < 1167609599) AS ties,
(SELECT COUNT(game_id) FROM nfl_games WHERE (((game_homeTeam = id) && game_homeScore < game_awayScore) || ((game_awayTeam = id) && (game_awayScore < game_homeScore))) && (game_dateTime > 1136073600) && (game_dateTime < 1167609599)) AS losses,
(SELECT wins + ties + losses) as total_games,
(SELECT SUM(game_homeScore) FROM nfl_games WHERE game_homeTeam = id && game_dateTime > 1136073600 && game_dateTime < 1167609599) AS home_points,
(SELECT SUM(game_awayScore) FROM nfl_games WHERE game_awayTeam = id && game_dateTime > 1136073600 && game_dateTime < 1167609599) AS away_points,
(SELECT IFNULL(home_points,0) + IFNULL(away_points,0)) AS points_for,
(SELECT FORMAT((points_for/total_games),3)) as game_average,
(SELECT FORMAT((wins/total_games),3)) as win_percentage,
(SELECT SUM(game_awayScore) FROM nfl_games WHERE (game_homeTeam = id) && game_dateTime > 1136073600 && game_dateTime < 1167609599) AS opp_home_points,
(SELECT SUM(game_homeScore) FROM nfl_games WHERE game_awayTeam = id && game_dateTime > 1136073600 && game_dateTime < 1167609599) AS opp_away_points,
(SELECT IFNULL(opp_home_points,0) + IFNULL(opp_away_points,0)) AS points_against
FROM nfl_teams
ORDER BY wins DESC, ties DESC, losses ASC
Thank you very much for any help you can give!