$query = dbquery("SELECT games.te_id, AVG( score ) AS pts, te_name, te_city
FROM (
SELECT ga_home_id AS te_id, ga_home_score AS score FROM prefix_games
UNION
SELECT ga_away_id AS te_id, ga_away_score AS score FROM prefix_games) AS games,
prefix_teams WHERE te_division='0' OR te_division='1' OR te_division='2'
AND games.te_id = prefix_teams.te_id
GROUP BY te_id
ORDER BY pts DESC");