SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Averaging out Rows

    Hi All

    I have a cities and a votes database.

    CITIES contains id,name,province fields.

    VOTES contains id,city,water,sanitation,elec,roads,health,housing fields.

    I am using the following statement to get initial data...

    Code MySQL:
    SELECT cities.name,cities.province,
        ROUND(AVG(votes.water)) as water,
        ROUND(AVG(votes.elec)) as elec,
        ROUND(AVG(votes.sanitation)) as sanitation,
        ROUND(AVG(votes.housing)) as housing,
        ROUND(AVG(votes.roads)) as roads,
        ROUND(AVG(votes.health)) as health,
        COUNT(votes.id) AS votes
    FROM cities
    INNER JOIN votes ON votes.city = cities.id
    GROUP BY cities.id
    ORDER BY votes DESC

    This produces the following results...


    What I now need to add to the above statement is to create a field that averages water,elec,sanitation,housing,roads and health from the generated results.

    Is that possible?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,273
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    you can try the WITH ROLLUP option on the GROUP BY clause, although you will ahve to ditch your ORDER BY clause
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •