SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict Avido's Avatar
    Join Date
    Jul 2003
    Location
    Kortrijk, Belgium, Europe, the world
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Aggregate with union?

    I have the following data:

    id | home_team | away_team | home_goals | away_goals
    ------------------------------------------------------------------
    1 | 1 | 2 | 5 | 2
    2 | 2 | 1 | 3 | 0

    I'm trying to build a query so i would get the following result

    team | average goals
    -----------------------------------
    1 | 2.5
    2 | 2.5

    I allready tried a union inside a aggregate function but that doesn't work. (not allowed?) Sombody has a hint for me?

  2. #2
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    696
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Code:
    select team,avg(goals)
     from 
      (select home_team as team, home_goals as goals
         from gamesTable
       union all
       select away_team, away_goals
         from gamesTable) dt
      group by team

  3. #3
    SitePoint Addict Avido's Avatar
    Join Date
    Jul 2003
    Location
    Kortrijk, Belgium, Europe, the world
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thx, that did it


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •